scripting_continued.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. .. _doc_scripting_continued:
  2. Scripting (continued)
  3. =====================
  4. Processing
  5. ----------
  6. Several actions in Godot are triggered by callbacks or virtual
  7. functions, so there is no need to check for writing code that runs all
  8. the time. Additionally, a lot can be done with animation players.
  9. However, it is still a very common case to have a script process on every
  10. frame. There are two types of processing, idle processing and fixed
  11. processing.
  12. Idle processing is activated with the
  13. :ref:`Node.set_process() <class_Node_set_process>`
  14. function. Once active, the :ref:`Node._process() <class_Node__process>`
  15. callback will be called every frame. Example:
  16. ::
  17. func _ready():
  18. set_process(true)
  19. func _process(delta):
  20. # do something...
  21. The delta parameter describes the time elapsed (in seconds, as
  22. floating point) since the previous call to _process().
  23. Fixed processing is similar, but only needed for synchronization with
  24. the physics engine.
  25. A simple way to test this is to create a scene with a single Label node,
  26. with the following script:
  27. ::
  28. extends Label
  29. var accum=0
  30. func _ready():
  31. set_process(true)
  32. func _process(delta):
  33. accum += delta
  34. set_text(str(accum))
  35. Which will show a counter increasing each frame.
  36. Groups
  37. ------
  38. Nodes can be added to groups (as many as desired per node). This is a
  39. simple yet useful feature for organizing large scenes. There are two
  40. ways to do this, the first is from the UI, from the Groups button under Node-panel:
  41. .. image:: /img/groups_in_nodes.PNG
  42. And the second from code. One useful example would be to tag scenes
  43. which are enemies.
  44. ::
  45. func _ready():
  46. add_to_group("enemies")
  47. This way, if the player is discovered sneaking into the secret base,
  48. all enemies can be notified about the alarm sounding, by using
  49. :ref:`SceneTree.call_group() <class_SceneTree_call_group>`:
  50. ::
  51. func _on_discovered():
  52. get_tree().call_group(0, "guards", "player_was_discovered")
  53. The above code calls the function "player_was_discovered" on every
  54. member of the group "guards".
  55. Optionally, it is possible to get the full list of "guards" nodes by
  56. calling
  57. :ref:`SceneTree.get_nodes_in_group() <class_SceneTree_get_nodes_in_group>`:
  58. ::
  59. var guards = get_tree().get_nodes_in_group("guards")
  60. More will be added about
  61. :ref:`SceneTree <class_SceneTree>`
  62. later.
  63. Notifications
  64. -------------
  65. Godot has a system of notifications. This is usually not needed for
  66. scripting, as it's too low level and virtual functions are provided for
  67. most of them. It's just good to know they exist. Simply
  68. add a
  69. :ref:`Object._notification() <class_Object__notification>`
  70. function in your script:
  71. ::
  72. func _notification(what):
  73. if (what == NOTIFICATION_READY):
  74. print("This is the same as overriding _ready()...")
  75. elif (what == NOTIFICATION_PROCESS):
  76. var delta = get_process_time()
  77. print("This is the same as overriding _process()...")
  78. The documentation of each class in the :ref:`Class Reference <toc-class-ref>`
  79. shows the notifications it can receive. However, for most cases GDScript
  80. provides simpler overrideable functions.
  81. Overrideable functions
  82. ----------------------
  83. Nodes provide many useful overrideable functions, which are described as
  84. follows:
  85. ::
  86. func _enter_tree():
  87. # When the node enters the _Scene Tree_, it becomes active
  88. # and this function is called. Children nodes have not entered
  89. # the active scene yet. In general, it's better to use _ready()
  90. # for most cases.
  91. pass
  92. func _ready():
  93. # This function is called after _enter_tree, but it ensures
  94. # that all children nodes have also entered the _Scene Tree_,
  95. # and became active.
  96. pass
  97. func _exit_tree():
  98. # When the node exits the _Scene Tree_, this function is called.
  99. # Children nodes have all exited the _Scene Tree_ at this point
  100. # and all became inactive.
  101. pass
  102. func _process(delta):
  103. # When set_process() is enabled, this function is called every frame.
  104. pass
  105. func _fixed_process(delta):
  106. # When set_fixed_process() is enabled, this is called every physics
  107. # frame.
  108. pass
  109. func _paused():
  110. # Called when game is paused. After this call, the node will not receive
  111. # any more process callbacks.
  112. pass
  113. func _unpaused():
  114. # Called when game is unpaused.
  115. pass
  116. As mentioned before, it's best to use these functions.
  117. Creating nodes
  118. --------------
  119. To create a node from code, just call the .new() method (like for any
  120. other class based datatype). Example:
  121. ::
  122. var s
  123. func _ready():
  124. s = Sprite.new() # create a new sprite!
  125. add_child(s) # add it as a child of this node
  126. To delete a node, be it inside or outside the scene, free() must be
  127. used:
  128. ::
  129. func _someaction():
  130. s.free() # immediately removes the node from the scene and frees it
  131. When a node is freed, it also frees all its children nodes. Because of
  132. this, manually deleting nodes is much simpler than it appears. Just free
  133. the base node and everything else in the sub-tree goes away with it.
  134. However, it might happen very often that we want to delete a node that
  135. is currently "blocked", because it is emitting a signal or calling a
  136. function. This will result in crashing the game. Running Godot
  137. in the debugger often will catch this case and warn you about it.
  138. The safest way to delete a node is by using
  139. :ref:`Node.queue_free() <class_Node_queue_free>`.
  140. This erases the node safely during idle.
  141. ::
  142. func _someaction():
  143. s.queue_free() # remove the node and delete it while nothing is happening
  144. Instancing scenes
  145. -----------------
  146. Instancing a scene from code is pretty easy and done in two steps. The
  147. first one is to load the scene from disk.
  148. ::
  149. var scene = load("res://myscene.scn") # will load when the script is instanced
  150. Preloading it can be more convenient sometimes, as it happens at parse
  151. time.
  152. ::
  153. var scene = preload("res://myscene.scn") # will load when parsing the script
  154. But 'scene' is not yet a node containing subnodes. It's packed in a
  155. special resource called :ref:`PackedScene <class_PackedScene>`.
  156. To create the actual node, the function
  157. :ref:`PackedScene.instance() <class_PackedScene_instance>`
  158. must be called. This will return the tree of nodes that can be added to
  159. the active scene:
  160. ::
  161. var node = scene.instance()
  162. add_child(node)
  163. The advantage of this two-step process is that a packed scene may be
  164. kept loaded and ready to use, so it can be used to create as many
  165. instances as desired. This is especially useful to quickly instance
  166. several enemies, bullets, etc. in the active scene.