nodes_and_scene_instances.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. Say 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 by their name. For example, if you rename the Sprite
  33. node into Skin, the call to get it would have to be ``get_node("Skin")``.
  34. Node paths
  35. ----------
  36. You're not limited to getting a direct child. The ``get_node()`` function
  37. supports paths, a bit like when working with a file browser. Add a slash to
  38. separate nodes.
  39. Take the following example scene, with the script attached to the UserInterface
  40. node.
  41. .. image:: img/nodes_and_scene_instances_ui_scene_example.png
  42. To get the Tween node, you would use the following code.
  43. .. tabs::
  44. .. code-tab:: gdscript GDScript
  45. var tween
  46. func _ready():
  47. tween = get_node("ShieldBar/Tween")
  48. .. code-tab:: csharp
  49. private Tween _tween;
  50. public override void _Ready()
  51. {
  52. base._Ready();
  53. _tween = GetNode<Tween>("ShieldBar/Tween");
  54. }
  55. .. note:: As with file paths, you can use ".." to get a parent node. The best
  56. practice is to avoid doing that though not to break encapsulation and
  57. keep your code organized. You can also start the path with a forward
  58. slash to make it absolute, in which case your topmost node would be
  59. "/root", the application's predefined root viewport.
  60. Syntactic sugar
  61. ~~~~~~~~~~~~~~~
  62. You can use two shorthands to shorten your code in GDScript: putting the
  63. ``onready`` keyword before a member variable makes it initialize right before
  64. the ``_ready()`` callback.
  65. .. code-block:: gdscript
  66. onready var sprite = get_node("Sprite")
  67. There is also a short notation for ``get_node()``: the dollar sign, "$". You
  68. place it before the name or path of the node you want to get.
  69. .. code-block:: gdscript
  70. onready var sprite = $Sprite
  71. onready var tween = $ShieldBar/Tween
  72. Creating nodes
  73. --------------
  74. To create a node from code, call its ``new()`` method like for any other
  75. class-based datatype.
  76. You can store the newly created node's reference in a variable and call
  77. ``add_child()`` to add it as a child of the node to which you attached the
  78. script.
  79. .. tabs::
  80. .. code-tab:: gdscript GDScript
  81. var sprite
  82. func _ready():
  83. var sprite = Sprite.new() # Create a new Sprite.
  84. add_child(sprite) # Add it as a child of this node.
  85. .. code-tab:: csharp
  86. private Sprite _sprite;
  87. public override void _Ready()
  88. {
  89. base._Ready();
  90. _sprite = new Sprite(); // Create a new Sprite.
  91. AddChild(_sprite); // Add it as a child of this node.
  92. }
  93. To delete a node and free it from memory, you can call its ``queue_free()``
  94. method. Doing so queues the node for deletion at the end of the current frame
  95. after it finished processing. At that point, the engine removes the node from
  96. the scene and frees the object in memory.
  97. .. tabs::
  98. .. code-tab:: gdscript GDScript
  99. sprite.queue_free()
  100. .. code-tab:: csharp
  101. _sprite.QueueFree();
  102. You can alternatively call ``free()`` to immediately destroy the node. You
  103. should do this with care as any reference to it will instantly become ``null``.
  104. We recommend using ``queue_free()`` unless you know what you're doing.
  105. When you free a node, it also frees all its children. Thanks to this, to delete
  106. an entire branch of the scene tree, you only have to free the topmost parent
  107. node.
  108. Instancing scenes
  109. -----------------
  110. Instancing a scene from code happens in two steps:
  111. 1. Loading the scene from the hard drive.
  112. 2. Creating an instance of the loaded :ref:`PackedScene <class_PackedScene>`
  113. resource.
  114. .. tabs::
  115. .. code-tab:: gdscript GDScript
  116. var scene = load("res://MyScene.tscn")
  117. .. code-tab:: csharp
  118. var scene = GD.Load<PackedScene>("res://MyScene.tscn");
  119. Preloading it can improve the user's experience as it happens when parsing the
  120. script. This feature is only available with GDScript.
  121. .. tabs::
  122. .. code-tab:: gdscript GDScript
  123. var scene = preload("res://MyScene.tscn")
  124. At that point, ``scene`` is a packed scene resource, not a node. To create the
  125. actual node, you need to call :ref:`PackedScene.instance()
  126. <class_PackedScene_method_instance>`. It returns a tree of nodes that you can
  127. add as a child.
  128. .. tabs::
  129. .. code-tab:: gdscript GDScript
  130. var instance = scene.instance()
  131. add_child(instance)
  132. .. code-tab:: csharp
  133. var instance = scene.();
  134. AddChild(instance);
  135. The advantage of this two-step process is you can keep a packed scene loaded and
  136. create new instances on the fly. For example, to quickly instance several
  137. enemies or bullets.