scripting_continued.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. Scripting (continued)
  2. =====================
  3. Processing
  4. ----------
  5. Several actions in Godot are triggered by callbacks or virtual
  6. functions, so there is no need to check for writing code that runs all
  7. time time. Additionally, a lot can be done with animation players.
  8. However,it is still a very common case to have a script process on every
  9. frame. There are two types of processing, idle processing and fixed
  10. processing.
  11. Idle processing is activated with the
  12. `Node.set\_process() <https://github.com/okamstudio/godot/wiki/class_node#set_process>`__
  13. function. Once active, the
  14. `Node.\_process() <https://github.com/okamstudio/godot/wiki/class_node>`__
  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 second.
  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 tne Groups button:
  41. .. image:: /img/groups.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. `SceneTree.call\_group() <https://github.com/okamstudio/godot/wiki/class_scenemainloop#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. `SceneTree.get\_nodes\_in\_group() <https://github.com/okamstudio/godot/wiki/class_scenemainloop#get_nodes_in_group>`__:
  58. ::
  59. var guards = get_tree().get_nodes_in_group("guards")
  60. More will be added about
  61. `SceneTree <https://github.com/okamstudio/godot/wiki/class_scenemainloop>`__
  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. `Object.\_notification() <https://github.com/okamstudio/godot/wiki/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 `class
  79. list <https://github.com/okamstudio/godot/wiki/class_class_list>`__
  80. shows the notifications it can receive. However, again, for most cases
  81. script provides simpler overrideable functions.
  82. Overrideable Functions
  83. ----------------------
  84. As mentioned before, it's better to use these functions. Nodes provide
  85. many useful overrideable functions, which are described as follows:
  86. ::
  87. func _enter_tree():
  88. 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.
  89. func _ready():
  90. pass # This function is called after _enter_tree, but it ensures that all children nodes have also entered the _Scene Tree_, and became active.
  91. func _exit_tree():
  92. 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.
  93. func _process(delta):
  94. pass # When set_process() is enabled, this is called every frame
  95. func _fixed_process(delta):
  96. pass # When set_fixed_process() is enabled, this is called every physics frame
  97. func _paused():
  98. pass # Called when game is paused, after this call, the node will not receive any more process callbacks
  99. func _unpaused():
  100. pass # Called when game is unpaused
  101. Creating Nodes
  102. --------------
  103. To create a node from code, just call the .new() method, (like for any
  104. other class based datatype). Example:
  105. ::
  106. var s
  107. func _ready():
  108. s = Sprite.new() # create a new sprite!
  109. add_child(s) #add it as a child of this node
  110. To delete a node, be it inside or outside the scene, free() must be
  111. used:
  112. ::
  113. func _someaction():
  114. s.free() # immediately removes the node from the scene and frees it
  115. When a node is freed, it also frees all it's children nodes. Because of
  116. this, manually deleting nodes is much simpler than it appears. Just free
  117. the base node and everything else in the sub-tree goes away with it.
  118. However, it might happen very often that we might want to delete a node
  119. that is currently "blocked" this means, the node is emitting a signal or
  120. calling a function. This will result in crashing the game. Running Godot
  121. in the debugger often will catch this case and warn you about it.
  122. The safest way to delete a node is by using
  123. `queue\_free() <https://github.com/okamstudio/godot/wiki/class_node#queue_free>`__
  124. instead. This erases the node during idle, safely.
  125. ::
  126. func _someaction():
  127. s.queue_free() # remove the node and delete it while nothing is happening
  128. Instancing Scenes
  129. -----------------
  130. Instancing a scene from code is pretty easy and done in two steps. The
  131. first one is to load the scene from disk.
  132. ::
  133. var scene = load("res://myscene.scn") # will load when the script is instanced
  134. Preloading it can be more convenient sometimes, as it happens at parse
  135. time.
  136. ::
  137. var scene = preload("res://myscene.scn") # will load when parsing the script
  138. But 'scene' is still not a node containing subnodes. It's packed in a
  139. special resource called
  140. `PackedScene <https://github.com/okamstudio/godot/wiki/class_packedscene>`__.
  141. To create the actual node, the function
  142. `PackedScene.instance() <https://github.com/okamstudio/godot/wiki/class_packedscene#instance>`__
  143. must be called. This will return the tree of nodes that can be added to
  144. the active scene:
  145. ::
  146. var node = scene.instance()
  147. add_child(node)
  148. The advantage of this two-step process is that a packed scene may be
  149. kept loaded and ready to use, so it can be used to create as many
  150. instances as desired. This is specially useful, for example, to instance
  151. several enemies, bullets, etc. quickly in the active scene.
  152. *Juan Linietsky, Ariel Manzur, Distributed under the terms of the `CC
  153. By <https://creativecommons.org/licenses/by/3.0/legalcode>`__ license.*