getting_started.rst 4.4 KB

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