scripting_continued.rst 6.9 KB

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