nodes_and_scene_instances.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. .. _doc_nodes_and_scene_instances:
  2. Nodes and scene instances
  3. =========================
  4. This guide explains how to get nodes, create nodes, add them as a child, and
  5. instantiate scenes from code.
  6. Getting nodes
  7. -------------
  8. You can get a reference to a node by calling the :ref:`Node.get_node()
  9. <class_Node_method_get_node>` method. For this to work, the child node must be
  10. present in the scene tree. Getting it in the parent node's ``_ready()`` function
  11. guarantees that.
  12. If, for example, you have a scene tree like this, and you want to get a reference to the
  13. Sprite and Camera2D nodes to access them in your script.
  14. .. image:: img/nodes_and_scene_instances_player_scene_example.png
  15. To do so, you can use the following code.
  16. .. tabs::
  17. .. code-tab:: gdscript GDScript
  18. var sprite
  19. var camera2d
  20. func _ready():
  21. sprite = get_node("Sprite")
  22. camera2d = get_node("Camera2D")
  23. .. code-tab:: csharp
  24. private Sprite _sprite;
  25. private Camera2D _camera2d;
  26. public override void _Ready()
  27. {
  28. base._Ready();
  29. _sprite = GetNode<Sprite>("Sprite");
  30. _camera2d = GetNode<Camera2D>("Camera2D");
  31. }
  32. Note that you get nodes using their name, not their type. Above, "Sprite" and
  33. "Camera2D" are the nodes' names in the scene.
  34. .. image:: img/nodes_and_scene_instances_sprite_node.png
  35. If you rename the Sprite node as Skin in the Scene dock, you have to change the
  36. line that gets the node to ``get_node("Skin")`` in the script.
  37. .. image:: img/nodes_and_scene_instances_sprite_node_renamed.png
  38. Node paths
  39. ----------
  40. When getting a reference to a node, you're not limited to getting a direct child. The ``get_node()`` function
  41. supports paths, a bit like when working with a file browser. Add a slash to
  42. separate nodes.
  43. Take the following example scene, with the script attached to the UserInterface
  44. node.
  45. .. image:: img/nodes_and_scene_instances_ui_scene_example.png
  46. To get the Tween node, you would use the following code.
  47. .. tabs::
  48. .. code-tab:: gdscript GDScript
  49. var tween
  50. func _ready():
  51. tween = get_node("ShieldBar/Tween")
  52. .. code-tab:: csharp
  53. private Tween _tween;
  54. public override void _Ready()
  55. {
  56. base._Ready();
  57. _tween = GetNode<Tween>("ShieldBar/Tween");
  58. }
  59. .. note:: As with file paths, you can use ".." to get a parent node. The best
  60. practice is to avoid doing that though not to break encapsulation.
  61. You can also start the path with a forward
  62. slash to make it absolute, in which case your topmost node would be
  63. "/root", the application's predefined root viewport.
  64. Syntactic sugar
  65. ~~~~~~~~~~~~~~~
  66. You can use two shorthands to shorten your code in GDScript. Firstly, putting the
  67. ``onready`` keyword before a member variable makes it initialize right before
  68. the ``_ready()`` callback.
  69. .. code-block:: gdscript
  70. onready var sprite = get_node("Sprite")
  71. There is also a short notation for ``get_node()``: the dollar sign, "$". You
  72. place it before the name or path of the node you want to get.
  73. .. code-block:: gdscript
  74. onready var sprite = $Sprite
  75. onready var tween = $ShieldBar/Tween
  76. Creating nodes
  77. --------------
  78. To create a node from code, call its ``new()`` method like for any other
  79. class-based datatype.
  80. You can store the newly created node's reference in a variable and call
  81. ``add_child()`` to add it as a child of the node to which you attached the
  82. script.
  83. .. tabs::
  84. .. code-tab:: gdscript GDScript
  85. var sprite
  86. func _ready():
  87. var sprite = Sprite.new() # Create a new Sprite.
  88. add_child(sprite) # Add it as a child of this node.
  89. .. code-tab:: csharp
  90. private Sprite _sprite;
  91. public override void _Ready()
  92. {
  93. base._Ready();
  94. _sprite = new Sprite(); // Create a new Sprite.
  95. AddChild(_sprite); // Add it as a child of this node.
  96. }
  97. To delete a node and free it from memory, you can call its ``queue_free()``
  98. method. Doing so queues the node for deletion at the end of the current frame
  99. after it has finished processing. At that point, the engine removes the node from
  100. the scene and frees the object in memory.
  101. .. tabs::
  102. .. code-tab:: gdscript GDScript
  103. sprite.queue_free()
  104. .. code-tab:: csharp
  105. _sprite.QueueFree();
  106. Before calling ``sprite.queue_free()``, the remote scene tree looks like this.
  107. .. image:: img/nodes_and_scene_instances_remote_tree_with_sprite.png
  108. After the engine freed the node, the remote scene tree doesn't display the
  109. sprite anymore.
  110. .. image:: img/nodes_and_scene_instances_remote_tree_no_sprite.png
  111. You can alternatively call ``free()`` to immediately destroy the node. You
  112. should do this with care as any reference to it will instantly become ``null``.
  113. We recommend using ``queue_free()`` unless you know what you're doing.
  114. When you free a node, it also frees all its children. Thanks to this, to delete
  115. an entire branch of the scene tree, you only have to free the topmost parent
  116. node.
  117. Instancing scenes
  118. -----------------
  119. Scenes are templates from which you can create as many reproductions as you'd
  120. like. This operation is called instancing, and doing it from code happens in two
  121. steps:
  122. 1. Loading the scene from the hard drive.
  123. 2. Creating an instance of the loaded :ref:`PackedScene <class_PackedScene>`
  124. resource.
  125. .. tabs::
  126. .. code-tab:: gdscript GDScript
  127. var scene = load("res://MyScene.tscn")
  128. .. code-tab:: csharp
  129. var scene = GD.Load<PackedScene>("res://MyScene.tscn");
  130. Preloading the scene can improve the user's experience as the load operation
  131. happens when the compiler reads the script and not at runtime. This feature is
  132. only available with GDScript.
  133. .. tabs::
  134. .. code-tab:: gdscript GDScript
  135. var scene = preload("res://MyScene.tscn")
  136. At that point, ``scene`` is a packed scene resource, not a node. To create the
  137. actual node, you need to call :ref:`PackedScene.instance()
  138. <class_PackedScene_method_instance>`. It returns a tree of nodes that you can
  139. as a child of your current node.
  140. .. tabs::
  141. .. code-tab:: gdscript GDScript
  142. var instance = scene.instance()
  143. add_child(instance)
  144. .. code-tab:: csharp
  145. var instance = scene.Instance();
  146. AddChild(instance);
  147. The advantage of this two-step process is you can keep a packed scene loaded and
  148. create new instances on the fly. For example, to quickly instance several
  149. enemies or bullets.