visual_script_basics.rst 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. .. _doc_visual_script:
  2. Visual Scripting
  3. ================
  4. Introduction
  5. ------------
  6. Visual Scripting is always a controversial topic, surronded by many misunderstandings.
  7. Programmers often think the aim of it is replacing their beloved code, so they
  8. act defensively against it. Artists and game designers think that, with it,
  9. they can write any game they want with little effort without the need of programmers.
  10. This situation is the result of really bad marketing from many products over time
  11. which, in their aim to capure the interest of artists and game designers, also
  12. made programmers feel like they are being replaced by visual tools.
  13. Truth is mostly, somewhat, in the middle. First of all, programmers can rest
  14. assured that Visual Scripting in no way makes code obsolete. Artists and game
  15. designers can also rest assured that they will be able to do a lot of things
  16. without programming.
  17. Visual Scripting is a tool designed to make the entry barrier to programming
  18. much lower. As code is more visual, it needs less abstract thinking to be
  19. understood. Any artist, animator, game designer, etc. can look at it and quickly
  20. grasp the flow of logic.
  21. The reason it does not make existing programming obsolete is, simply, that it does not scale as well.
  22. It takes considerably more time to create code with it, and it's often more difficult
  23. to modify than just writing a few characters.
  24. With the misunderstanding cleared up, the question that remains is what are the practical
  25. uses is for Visual Scripting.
  26. The most common use cases are are as follows:
  27. * Beginners into game development who want to learn an engine but have no programming experience yet.
  28. * Artists and Game Designers who have no experience in programming and want to create quick prototypes or simple games.
  29. * Programmers working in a team that want to make part of the game logic available to Artists or Game Designers in order to offload some of their work.
  30. These scenarios are far more common than one might think, so this is why Godot has added this feature.
  31. Visual Scripting in Godot
  32. -------------------------
  33. As with everything in Godot, we prioritize a good experience over copying or integrating third party solutions
  34. which might not fit nicely in the current workflow. This led us to write our own version of how we believe
  35. this feature would work best with the engine.
  36. In Godot, a Visual Script fits smoothly together with regular scripts in the Editor tab
  37. .. image:: /img/visual_script1.png
  38. In fact, Visual Scripting integrates so well to Godot that It's hard to believe it was added only
  39. in version 3.0. This is because, when editing, the rest of Godot panels and
  40. docks act like a palette from where you can drag and drop all sorts of information to the script canvas:
  41. .. image:: /img/visual_script2.png
  42. Creating a Script
  43. -----------------
  44. Creating scripts works the same as with other scripting languages: Just select any node in the scene
  45. and push the "New Script" button at the top right corner of the Scene Tree dock:
  46. .. image:: /img/visual_script3.png
  47. Once it opens, the script type "Visual Script" must be selected from the drop down list. The script extension
  48. must be ".vs" (for Visual Script!).
  49. .. image:: /img/visual_script4.png
  50. Finally, the script editor will open, allowing to start the editing of the visual script:
  51. .. image:: /img/visual_script5.png
  52. Adding a Function
  53. -----------------
  54. Unlike other visual scripting implementations, Visual Scripting in Godot is heavily based on functions.
  55. This happens because it uses the same interface to communicate with the engine as other scripting engines.
  56. In Godot, the scripting interface is universal and all implementations conform to it.
  57. A function is an individual canvas with nodes connected.
  58. A single script can contain many functions, each of which will have a canvas of it's own. This makes scripts
  59. more organized.
  60. There are three main ways to add functions in a script:
  61. Overriding a Virtual Function
  62. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  63. Most types of nodes and other types of objects in Godot contain virtual functions. These are functions that
  64. will be called (run your code) when something happens and can be looked up in the reference. Virtual functions
  65. are listed when pressing the "Override" icon in the member panel:
  66. .. image:: /img/visual_script6.png
  67. In the following example, a function will be executed when the node is loaded and added to the running scene.
  68. For this, the _ready() virtual method will be overriden:
  69. .. image:: /img/visual_script7.png
  70. Finally, a canvas appears for this function, showing the override:
  71. .. image:: /img/visual_script8.png
  72. As some functions expect you to return a value, they will also add a return node where such value is supposed to be
  73. provided
  74. .. image:: /img/visual_script9.png
  75. Connecting a Signal to a Function
  76. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  77. Nodes in a tree emit signals when something happens. Godot uses signals for all sorts of things.
  78. A typical example would be a button that emits a "pressed" signal when actually pressed.
  79. For this, a node must be selected and the Node tab opened. This will allow inspecting the signals.
  80. Once they are displayed, connect the "pressed" signal:
  81. .. image:: /img/visual_script10.png
  82. This will open the connection dialog. In this dialog, we must select the node where the signal be
  83. connected to, and the function that will receive the signal:
  84. .. image:: /img/visual_script11.png
  85. If this is done right, a new function will be created in our script and a signal will automatically be
  86. connected to it:
  87. .. image:: /img/visual_script12.png
  88. Creating a Function Manually
  89. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. The last way to create functions is to do it manualy. In general this is not as common unless you
  91. really need it. Custom functions work when another (or the same) script calls them manually.
  92. The main use case for this is to separate a function into more, or reusing your visual code.
  93. To create a function manually, push the big "Plus" button, and a new function will be added
  94. with a default name:
  95. .. image:: /img/visual_script13.png
  96. This will add a new function, which can be renamed by simply double clicking it's name:
  97. .. image:: /img/visual_script14.png
  98. To edit the "arguments" this function can get (the values you pass to it when you call this function),
  99. simply click the Function node and check the inspector:
  100. .. image:: /img/visual_script15.png
  101. More on that will be explained later in this document.
  102. Nodes and Terminology
  103. ----------------------
  104. Before continuing, it must be noted that the *Node* termonology needs to be used with care.
  105. When refering to *Visual Script Nodes* (or generally just *Nodes*) this text will refer to the little boxes you connect with lines,
  106. which are part of a graph. When refering to just *Scene Nodes*, it is implied that the elements
  107. that make up a Scene are being refered, which ar part of a tere. Their naming is similar, but their function is different.
  108. When refering to *Node* here, it will be implied that a *Visual Script Node* is refered to unless indicated otherwise.
  109. .. image:: /img/visual_script16.png
  110. Node Properties
  111. ---------------
  112. Like in most visual scripting implementations, each node has editable properties. In Godot, though, we try to avoid
  113. bloating the nodes with editable controls for the sake of readability.
  114. Nodes still display the required information as text, but editing is done via the *Inspector*. To edit them, just
  115. select andy node and edit it's properties in the *Inspector*.
  116. Ports and Connections
  117. --------------------
  118. Programming in Godot Visual Scripting is done via *Nodes* and *Port Connections* inside each function.
  119. Ports
  120. ~~~~~
  121. Nodes in Godot Visual Scripting have *Ports*. These are endpoints that appear to the
  122. left and right of nodes and which can be used to make *Connnections*:
  123. There are two types of *Ports*: *Sequence* and *Data*.
  124. .. image:: /img/visual_script17.png
  125. *Sequence Ports* indicate the order in which operations are executed.
  126. Typically when a *Node* is done processing, it will go to the next node from one of the ports at the right.
  127. If nothing is connected the function may end, or another output *Sequence Port* might be tried (this depends on the node).
  128. Thanks to this, it's easy to understand the logic within a function by just following the white lines.
  129. Not every *Node* has *Sequence Ports*. In fact, most do not.
  130. *Data Ports* ports contain typed values. Types can be any regular Godot types,
  131. such as a boolean, an integer, a string, a Vector3, an array, any Object or Scene Node, etc.
  132. A *Data Port* on the right side of a node is considered an output, while,
  133. a port on the left side is an input. Connecting them allows to transfer information from a node to the next.
  134. Not all *Data Port types are compatible and will allow connections, though.
  135. Pay special attention to colors and icons, as each type has a different representation:
  136. .. image:: /img/visual_script18.png
  137. Connections
  138. ~~~~~~~~~~~
  139. Connecting is a relatively simple process. Just drag an *Output Port* towards an *Input Port*.
  140. .. image:: /img/visual_script_connect.gif
  141. Disconnecting takes a bit more practice. Disconnecting in *Data Ports* happens by
  142. dragging the *Input* away, while for *Sequence Ports*, this happens by dragging the *Output* away.
  143. .. image:: /img/visual_script_disconnect.gif
  144. This may seem strange at the beginning, but it happens because *Data Ports* are 1:N
  145. (A single output port can connect to many inputs), while *Sequence Ports* are N:1
  146. (Many sequence outputs can be connected to a single input).
  147. Connecting to empty space (drag to connect but unpress over empty space) is also context sensitive, it will supply
  148. a list of most common operations. For sequences, it will be conditional nodes:
  149. .. image:: /img/visual_script52.png
  150. While, for data, a contextual set/get/call menu will open:
  151. .. image:: /img/visual_script53.png
  152. Adding Nodes
  153. ------------
  154. Finally! We got to the fun part! But, before explaining in more detail what each type of node does,
  155. let's take a short look at how nodes are most commonly added and dealt with.
  156. Accessing Scene Nodes
  157. ~~~~~~~~~~~~~~~~~~~~~
  158. One of the most common tasks is accessing Scene Tree Nodes (again, not to mistake with *Visual Script Nodes*).
  159. Dragging from the Scene Tree and dropping into the canvas, by default, will ask you to *call a method* (sometimes refered to as *member function*) on
  160. this node.
  161. .. image:: /img/visual_script19.png
  162. While accessing properties is desired in most cases (more on that below), sometimes *calling methods* can be useful too.
  163. Methods execute specific actions on objects. In the above case, the mouse pointer can be warped to a position in local
  164. coordinates to the control. Another common use case is queueing a node for deletion, which is done with the *queue_free* method.
  165. .. image:: /img/visual_script20.png
  166. Care must be taken that this only works if the scene being edited contains your *Visual Script* in one of the nodes! Otherwise, a warning will be shown.
  167. Accessing Scene Node Properties
  168. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  169. This will be by far the most common way to *Scene Nodes* in Visual Scripting. Simply select a *Scene Node* from the *Scene Tree*,
  170. go the the inspector, find *The Name* of the property you want to edit (hint, *not* the value!) and drag it to the canvas:
  171. .. image:: /img/visual_script21.png
  172. The result is that this value can be changed from your script by writing to a *Data Port*.
  173. If instead reading this value is desired, just drag the node again but hold the *Control* key (or Command on Mac). This will create a getter:
  174. .. image:: /img/visual_script22.png
  175. In this case, the value can be read from a *Data Port*.
  176. Variables
  177. ~~~~~~~~~
  178. Variables are memory containers local the script, which can hold a value. This value can be read from any of the functions of the script, or
  179. from other scripts via the method described in the previous step.
  180. Adding a Variable is simple, just push the "Plus" Button on the *Variables* section of the members panel. Afterwards, doubleclick the
  181. new variable to rename it:
  182. .. image:: /img/visual_script23.png
  183. Right clicking the variable allows to configure the type, as well as other properties:
  184. .. image:: /img/visual_script24.png
  185. .. image:: /img/visual_script25.png
  186. As it can be seen above, the type and initial value of the variable can be changed, as well as some property hints (@TODO, document this).
  187. Ticking the "Export" options makes the variable visible in the property editor when selecting the node. This makes it available also to
  188. othe scripts via the method described in the previous step.
  189. .. image:: /img/visual_script28.png
  190. To use the variable in the script, simply drag it to the canvas to create a getter:
  191. .. image:: /img/visual_script26.png
  192. Likewise, hold *Control* (*Command* on Mac) to drop a setter:
  193. .. image:: /img/visual_script27.png
  194. Signals
  195. ~~~~~~~
  196. In the *Connecting Signals to a Function* item we have already learned about *Signals*. It is also possible to create your own
  197. signals in a script and use them. For this, just do the same steps you did for variables in the previous step, except for *Signals*:
  198. .. image:: /img/visual_script29.png
  199. Signal can also be edited via right click menu to customize arguments:
  200. .. image:: /img/visual_script30.png
  201. The signal you have just created will also show together with the other node signals, this allows to eventually connect to it from another script
  202. from another *Scene Node*:
  203. .. image:: /img/visual_script31.png
  204. Finally, to emit the signal, simply drag it to the canvas:
  205. .. image:: /img/visual_script32.png
  206. Remember that emitting a signal is a sequenced operation, so it must come from a Sequence port.
  207. Adding More Nodes
  208. -----------------
  209. Now that the basics are covered, let's discuss the large amount of utilitary nodes available for your canvas!
  210. Below the member panel, exists the list of all available node types:
  211. .. image:: /img/visual_script33.png
  212. Any of them can be dragged to the scene. Remember that, unlike the nodes previously discussed (e.g. dragging a property
  213. from the inspector sets the context to the node being edited automatically), these are
  214. added without any "contextual" information, so this has to be done manually.
  215. .. image:: /img/visual_script34.png
  216. Remember that you can check the class reference for what each node does, as they are documented there. That mentioned,
  217. a brief overview of node types follows:
  218. Constants
  219. ~~~~~~~~~
  220. Constant nodes are nodes that provide values that, while not changing over time, can be useful as reference values.
  221. Most of the time they are integer or float.
  222. .. image:: /img/visual_script36.png
  223. Of interest here are mainly three nodes. The first one is "Constant" which allows to select any value of any type as constant,
  224. from an integer (42) to a String ("Hello!"). In general this node is not used that often because of default input
  225. values in *Data Ports*, but it's good to know it exists.
  226. The second is the GlobalConstant node, which contains a long list of constants for global types in Godot. In there
  227. you can find some useful constants to refer to key names, joystick or mouse buttons, etc.
  228. The third one is MathConstant, which provides typical mathematical constants such as PI, E, etc.
  229. Data
  230. ~~~~
  231. Data nodes deal with all sorts of access to information. Any information in Godot is accessed via these nodes, so
  232. they are some of the most important ones to use and pretty diverse.
  233. .. image:: /img/visual_script37.png
  234. There are many types of nodes of interest here, so a short attempt to describe them will follow:
  235. Action
  236. ^^^^^^
  237. Action nodes are vital when dealing with input from a device. You can read more about actions in the (@TODO ACTION TUTE LINK).
  238. In the following example below, the control is moved to the right when the "move_right" action is pressed.
  239. .. image:: /img/visual_script38.png
  240. Engine Singleton
  241. ^^^^^^^^^^^^^^^^
  242. Engine singletons are global interfaces (meaning they can be accessed without a reference, unlike Scene Nodes, they are always available).
  243. They have several purposes, but in general they are useful for low level access or OS-Related access.
  244. .. image:: /img/visual_script39.png
  245. Remember that dragging a connection to empty space will help you call functions or set/get properties on these:
  246. .. image:: /img/visual_script40.png
  247. Local Variables
  248. ^^^^^^^^^^^^^^^
  249. These are nodes you can use as temporary storage for your graphs. Just make sure they all have the same name and type when using them
  250. and they will reference the same piece of memory.
  251. .. image:: /img/visual_script41.png
  252. As it can be seen above, there are two nodes available: A simple getter, and a sequenced getter (setting requires a sequence port).
  253. Scene Node
  254. ^^^^^^^^^^
  255. This is just a reference to a node in the tree, but it's easier to use this node by just dragging the actual node
  256. from the scene tree to the canvas (this will create it and configure it).
  257. Self
  258. ^^^^
  259. In some rare ocassions, it may be desired to pass this Scene Node as argument.
  260. It can be used to call functions and set/get properties, but it's easier to just
  261. drag nodes (or event he node itself that has the script) from the Scene Tree to the canvas for this.
  262. SceneTree
  263. ^^^^^^^^^
  264. This node is similar to the Singleton node because it references the SceneTree, which contains the active scene.
  265. SceneTree, however, only works when the node is sitting in the scene and active, otherwise accessing it will
  266. return as an error.
  267. SceneTree allows for many low level things, like setting stretch options, calling groups, make timers, or even
  268. load another scene. It's a good class to get familiar with.
  269. Preload
  270. ^^^^^^^
  271. This does the same function as preload() in GDScript. It maintains this resource loaded and ready to use. Rather than
  272. instancing the node, it's simpler to just drag the desired resource from the filesystem dock to the canvas.
  273. Resource Path
  274. ^^^^^^^^^^^^^
  275. This node is a simple helper to get a string with a path to a resource you can pick. It's useful in functions that
  276. load things from disk.
  277. Comment
  278. ^^^^^^^
  279. A Comment node works as a node you can resize to put around other nodes. It will not try to get focus or be brought
  280. to top when seleting it. It can also be used to write text on it.
  281. .. image:: /img/visual_script42.png
  282. Flow Control
  283. ~~~~~~~~~~~~
  284. Flow control nodes are all sequenecd, and allow the execution take different branches, usually depending on a
  285. given condition.
  286. .. image:: /img/visual_script43.png
  287. Condition
  288. ^^^^^^^^^
  289. This is a simple node that checks a bool port. If true, it will go via the "true" sequence port. If false,
  290. the second. After going for either of them, it goes via the "done" port. Leaving sequence
  291. ports disconnected is fine if no all of them are used.
  292. Iterator
  293. ^^^^^^^^
  294. Some data types in Godot (ie, arrays, dictionaries) are iterable. This means that a bit of code can run
  295. for each element that it has.
  296. The Iterator node goes through all elements and, for each of them, it goes via the "each" sequence port,
  297. making the element available in the "elem" data port.
  298. When done, it goes via the "exit" sequence port.
  299. Return
  300. ^^^^^^
  301. Some functions can return values. In general for virtual ones, Godot will add the Return node for you.
  302. A return node forces the function to end.
  303. Sequence
  304. ^^^^^^^^
  305. This node is useful mostly for organizing your graph. It calls it's sequence ports in order.
  306. TypeCast
  307. ^^^^^^^^
  308. This is a very useful and commonly used node. You can use it to cast arguments or other objects
  309. to the type you desire. Afterwards, you can even drag the obj output to get full completion.
  310. .. image:: /img/visual_script55.png
  311. It is also possible to cast to a script, which will allow to complete script properties and functions:
  312. .. image:: /img/visual_script54.png
  313. Switch
  314. ^^^^^^
  315. The Switch node is similar to the Condition node, but it matches many values at the same time.
  316. While
  317. ^^^^^
  318. This is a more primitive form of iteration. "repeat" sequence output will be called as long as
  319. the condition in the "cond" data port is met.
  320. Functions
  321. ~~~~~~~~~
  322. Functions are simple helpers, most of the time deterministic. They take some arguments as
  323. input and return an output. They are almost never sequenced.
  324. Built-In
  325. ^^^^^^^^
  326. There is a list of built in helpers. The list is almost identical to the one from GDScript (@TODO, link to gdscript methods?)
  327. Most of them are mathematical functions, but others can be very useful helpers. Just make sure to take a look at the list
  328. at some point.
  329. By Type
  330. ^^^^^^^
  331. Those are the methods available to basic types. For example, if you want a dot-product, you can search for "dot" intead of the Vector3 category.
  332. In most cases just search the list of nodes, it should be faster.
  333. Call
  334. ^^^^
  335. This is the generic calling node. It is rarely used directly but by dragging to empty space on an already cofigured node.
  336. Constructors
  337. ^^^^^^^^^^^^
  338. These are all the functions needed to create godot basic datatypes. If you need to, for example, create a Vector3 out of 3 floats, a
  339. constructor must be used.
  340. .. image:: /img/visual_script44.png
  341. Destructor
  342. ^^^^^^^^^^
  343. This is the opposite to Constructor, it allows to separate any basic type (ie, Vector3) into it's sub-elements.
  344. .. image:: /img/visual_script45.png
  345. Emit Signal
  346. ^^^^^^^^^^^
  347. Emits signals from any object.
  348. Emit Signal
  349. ^^^^^^^^^^^
  350. Emits signals from any object. In general not very useful, as dragging a signal to the canvas works better.
  351. Get/Set
  352. ^^^^^^^
  353. Generic Getter/Setter node. Dragging properties from the Inspector works better, as they appear properly configured on drop.
  354. Wait
  355. ^^^^
  356. The Wait nodes will suspend execution of the function until something happens (many frames can pass until resuming, in fact).
  357. Default nodes allow you to wait for a frame to pass, a fixed frame or a given amount of time until execution is resumed.
  358. Yield
  359. ^^^^^
  360. This node completely suspends the execution of the script, and it wil make the function return a value that can be used to resume execution.
  361. Yield Signal
  362. ^^^^^^^^^^^^
  363. Same as Yield, but will wait until a given signal is emitted.
  364. Index
  365. ~~~~~
  366. Generic indexing operator, not often used but it's good that exists just in case.
  367. Operators
  368. ~~~~~~~~~
  369. These are mostly generic operators such as addition, multiplication, comparison, etc.
  370. By default, these mostly accept any datatype (and will error in run-time if the types
  371. feeded do not match for the operator). Is is always recommended to set the right
  372. type for operators to catch errors faster and make the graph easier to read.
  373. .. image:: /img/visual_script46.png
  374. Expression Node
  375. ^^^^^^^^^^^^^^^
  376. Among the operators, the *Expression* node is the most powerful. If well used, it allows to enormously simplify
  377. visual scripts that are math or logic heavy. Just type any expression on it and it will be executed in real-time.
  378. Expression nodes can:
  379. - Perform math and logic expressions based on custom inputs (eg: "a*5+b", where a and b are custom inputs):
  380. .. image:: /img/visual_script47.png
  381. - Access local variables or properties:
  382. .. image:: /img/visual_script48.png
  383. - Use most of the existing built-in functions and available to GDScript, such as sin(),cos(),print(), as well as constructors, such as Vector3(x,y,z),Rect2(..), etc.:
  384. .. image:: /img/visual_script49.png
  385. - Call API functions:
  386. .. image:: /img/visual_script50.png
  387. - Use sequenced mode, which makes more sense in case of respecting the processing order:
  388. .. image:: /img/visual_script51.png