scripting_continued.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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, for example, to
  43. tag scenes which are enemies.
  44. ::
  45. func _ready():
  46. add_to_group("enemies")
  47. This way, if the player, sneaking into the secret base, is discovered,
  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 to be
  66. used from scripting, as it's too low level and virtual functions are
  67. provided for most of them. It's just good to know they exists. 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, again, for most cases
  80. script provides simpler overrideable functions.
  81. Overrideable functions
  82. ----------------------
  83. As mentioned before, it's better to use these functions. Nodes provide
  84. many useful overrideable functions, which are described as follows:
  85. ::
  86. func _enter_tree():
  87. # When the node enters the _Scene Tree_, it become 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. Creating nodes
  117. --------------
  118. To create a node from code, just call the .new() method, (like for any
  119. other class based datatype). Example:
  120. ::
  121. var s
  122. func _ready():
  123. s = Sprite.new() # create a new sprite!
  124. add_child(s) # add it as a child of this node
  125. To delete a node, be it inside or outside the scene, free() must be
  126. used:
  127. ::
  128. func _someaction():
  129. s.free() # immediately removes the node from the scene and frees it
  130. When a node is freed, it also frees all its children nodes. Because of
  131. this, manually deleting nodes is much simpler than it appears. Just free
  132. the base node and everything else in the sub-tree goes away with it.
  133. However, it might happen very often that we might want to delete a node
  134. that is currently "blocked" this means, the node is emitting a signal or
  135. calling a function. This will result in crashing the game. Running Godot
  136. in the debugger often will catch this case and warn you about it.
  137. The safest way to delete a node is by using
  138. :ref:`Node.queue_free() <class_Node_queue_free>`
  139. instead. This erases the node during idle, safely.
  140. ::
  141. func _someaction():
  142. s.queue_free() # remove the node and delete it while nothing is happening
  143. Instancing scenes
  144. -----------------
  145. Instancing a scene from code is pretty easy and done in two steps. The
  146. first one is to load the scene from disk.
  147. ::
  148. var scene = load("res://myscene.scn") # will load when the script is instanced
  149. Preloading it can be more convenient sometimes, as it happens at parse
  150. time.
  151. ::
  152. var scene = preload("res://myscene.scn") # will load when parsing the script
  153. But 'scene' is still not a node containing subnodes. It's packed in a
  154. special resource called :ref:`PackedScene <class_PackedScene>`.
  155. To create the actual node, the function
  156. :ref:`PackedScene.instance() <class_PackedScene_instance>`
  157. must be called. This will return the tree of nodes that can be added to
  158. the active scene:
  159. ::
  160. var node = scene.instance()
  161. add_child(node)
  162. The advantage of this two-step process is that a packed scene may be
  163. kept loaded and ready to use, so it can be used to create as many
  164. instances as desired. This is specially useful, for example, to instance
  165. several enemies, bullets, etc. quickly in the active scene.