getting_started.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. .. _doc_getting_started_visual_script:
  2. Visual Scripting
  3. ================
  4. Visual Scripting in Godot
  5. -------------------------
  6. As with everything in Godot, we prioritize a good experience over copying or integrating third party solutions
  7. which might not fit nicely in the current workflow. This led us to write our own version of how we believe
  8. this feature would work best with the engine.
  9. In Godot, a Visual Script fits smoothly together with regular scripts in the Editor tab
  10. .. image:: img/visual_script1.png
  11. In fact, Visual Scripting integrates so well to Godot that it's hard to believe it was added only
  12. in version 3.0. This is because, when editing, the rest of Godot panels and docks act like a
  13. palette from where you can drag and drop all sorts of information to the script canvas:
  14. .. image:: img/visual_script2.png
  15. Creating a Script
  16. -----------------
  17. Creating scripts works the same as with other scripting languages: Just select any node in the scene
  18. and push the "New Script" button at the top right corner of the Scene Tree dock:
  19. .. image:: img/visual_script3.png
  20. Once it opens, the script type "Visual Script" must be selected from the drop down list. The script extension
  21. must be ".vs" (for Visual Script!).
  22. .. image:: img/visual_script4.png
  23. Finally, the Script Editor will open, allowing to start the editing of the visual script:
  24. .. image:: img/visual_script5.png
  25. Adding a Function
  26. -----------------
  27. Unlike other visual scripting implementations, Visual Scripting in Godot is heavily based on functions.
  28. This happens because it uses the same interface to communicate with the engine as other scripting engines.
  29. In Godot, the scripting interface is universal and all implementations conform to it.
  30. A function is an individual canvas with nodes connected.
  31. A single script can contain many functions, each of which will have a canvas of its own, allowing for more organization.
  32. There are three main ways to add functions in a script:
  33. Overriding a Virtual Function
  34. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. Most types of nodes and other types of objects in Godot contain virtual functions. These are functions that
  36. will be called (run your code) when something happens and can be looked up in the reference. Virtual functions
  37. are listed when pressing the "Override" icon in the member panel:
  38. .. image:: img/visual_script6.png
  39. In the following example, a function will be executed when the node is loaded and added to the running scene.
  40. For this, the _ready() virtual method will be overridden:
  41. .. image:: img/visual_script7.png
  42. Finally, a canvas appears for this function, showing the override:
  43. .. image:: img/visual_script8.png
  44. As some functions expect you to return a value, they will also add a return node where such value is supposed to be
  45. provided:
  46. .. image:: img/visual_script9.png
  47. Connecting a Signal to a Function
  48. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  49. Nodes in a tree emit signals when something happens. Godot uses signals for all sorts of things.
  50. A typical example would be a button that emits a "pressed" signal when actually pressed.
  51. For this, a node must be selected and the Node tab opened. This will allow inspecting the signals.
  52. Once they are displayed, connect the "pressed" signal:
  53. .. image:: img/visual_script10.png
  54. This will open the connection dialog. In this dialog, you must select the node where the signal will be
  55. connected to, and the function that will receive the signal:
  56. .. image:: img/visual_script11.png
  57. If this is done right, a new function will be created in our script and a signal will automatically be
  58. connected to it:
  59. .. image:: img/visual_script12.png
  60. Creating a Function Manually
  61. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  62. The last way to create functions is to do it manually. In general this is not as common unless you
  63. really need it. Custom functions work when another (or the same) script calls them manually.
  64. The main use case for this is to separate a function into more, or reusing your visual code.
  65. To create a function manually, push the big "Plus" button, and a new function will be added
  66. with a default name:
  67. .. image:: img/visual_script13.png
  68. This will add a new function, which can be renamed by simply double clicking its name:
  69. .. image:: img/visual_script14.png
  70. To edit the "arguments" this function can get (the values you pass to it when you call this function),
  71. simply click the Function node and check the inspector:
  72. .. image:: img/visual_script15.png
  73. More on that will be explained later in this document.