|
@@ -17,7 +17,7 @@ guarantees that.
|
|
If, for example, you have a scene tree like this, and you want to get a reference to the
|
|
If, for example, you have a scene tree like this, and you want to get a reference to the
|
|
Sprite2D and Camera2D nodes to access them in your script.
|
|
Sprite2D and Camera2D nodes to access them in your script.
|
|
|
|
|
|
-.. image:: img/nodes_and_scene_instances_player_scene_example.png
|
|
|
|
|
|
+.. image:: img/nodes_and_scene_instances_player_scene_example.webp
|
|
|
|
|
|
To do so, you can use the following code.
|
|
To do so, you can use the following code.
|
|
|
|
|
|
@@ -28,7 +28,7 @@ To do so, you can use the following code.
|
|
var camera2d
|
|
var camera2d
|
|
|
|
|
|
func _ready():
|
|
func _ready():
|
|
- sprite2d = get_node("Sprite")
|
|
|
|
|
|
+ sprite2d = get_node("Sprite2D")
|
|
camera2d = get_node("Camera2D")
|
|
camera2d = get_node("Camera2D")
|
|
|
|
|
|
.. code-tab:: csharp
|
|
.. code-tab:: csharp
|
|
@@ -40,19 +40,19 @@ To do so, you can use the following code.
|
|
{
|
|
{
|
|
base._Ready();
|
|
base._Ready();
|
|
|
|
|
|
- _sprite2D = GetNode<Sprite2D>("Sprite");
|
|
|
|
|
|
+ _sprite2D = GetNode<Sprite2D>("Sprite2D");
|
|
_camera2D = GetNode<Camera2D>("Camera2D");
|
|
_camera2D = GetNode<Camera2D>("Camera2D");
|
|
}
|
|
}
|
|
|
|
|
|
Note that you get nodes using their name, not their type. Above, "Sprite2D" and
|
|
Note that you get nodes using their name, not their type. Above, "Sprite2D" and
|
|
"Camera2D" are the nodes' names in the scene.
|
|
"Camera2D" are the nodes' names in the scene.
|
|
|
|
|
|
-.. image:: img/nodes_and_scene_instances_sprite_node.png
|
|
|
|
|
|
+.. image:: img/nodes_and_scene_instances_sprite_node.webp
|
|
|
|
|
|
If you rename the Sprite2D node as Skin in the Scene dock, you have to change the
|
|
If you rename the Sprite2D node as Skin in the Scene dock, you have to change the
|
|
line that gets the node to ``get_node("Skin")`` in the script.
|
|
line that gets the node to ``get_node("Skin")`` in the script.
|
|
|
|
|
|
-.. image:: img/nodes_and_scene_instances_sprite_node_renamed.png
|
|
|
|
|
|
+.. image:: img/nodes_and_scene_instances_sprite_node_renamed.webp
|
|
|
|
|
|
Node paths
|
|
Node paths
|
|
----------
|
|
----------
|
|
@@ -64,27 +64,27 @@ separate nodes.
|
|
Take the following example scene, with the script attached to the UserInterface
|
|
Take the following example scene, with the script attached to the UserInterface
|
|
node.
|
|
node.
|
|
|
|
|
|
-.. image:: img/nodes_and_scene_instances_ui_scene_example.png
|
|
|
|
|
|
+.. image:: img/nodes_and_scene_instances_ui_scene_example.webp
|
|
|
|
|
|
-To get the Tween node, you would use the following code.
|
|
|
|
|
|
+To get the AnimationPlayer node, you would use the following code.
|
|
|
|
|
|
.. tabs::
|
|
.. tabs::
|
|
.. code-tab:: gdscript GDScript
|
|
.. code-tab:: gdscript GDScript
|
|
|
|
|
|
- var tween
|
|
|
|
|
|
+ var animation_player
|
|
|
|
|
|
func _ready():
|
|
func _ready():
|
|
- tween = get_node("ShieldBar/Tween")
|
|
|
|
|
|
+ animation_player = get_node("ShieldBar/AnimationPlayer")
|
|
|
|
|
|
.. code-tab:: csharp
|
|
.. code-tab:: csharp
|
|
|
|
|
|
- private Tween _tween;
|
|
|
|
|
|
+ private AnimationPlayer _animationPlayer;
|
|
|
|
|
|
public override void _Ready()
|
|
public override void _Ready()
|
|
{
|
|
{
|
|
base._Ready();
|
|
base._Ready();
|
|
|
|
|
|
- _tween = GetNode<Tween>("ShieldBar/Tween");
|
|
|
|
|
|
+ _animationPlayer = GetNode<AnimationPlayer>("ShieldBar/AnimationPlayer");
|
|
}
|
|
}
|
|
|
|
|
|
.. note:: As with file paths, you can use ".." to get a parent node. The best
|
|
.. note:: As with file paths, you can use ".." to get a parent node. The best
|
|
@@ -110,7 +110,7 @@ place it before the name or path of the node you want to get.
|
|
.. code-block:: gdscript
|
|
.. code-block:: gdscript
|
|
|
|
|
|
@onready var sprite2d = $Sprite2D
|
|
@onready var sprite2d = $Sprite2D
|
|
- @onready var tween = $ShieldBar/Tween
|
|
|
|
|
|
+ @onready var animation_player = $ShieldBar/AnimationPlayer
|
|
|
|
|
|
Creating nodes
|
|
Creating nodes
|
|
--------------
|
|
--------------
|
|
@@ -159,12 +159,12 @@ the scene and frees the object in memory.
|
|
|
|
|
|
Before calling ``sprite2d.queue_free()``, the remote scene tree looks like this.
|
|
Before calling ``sprite2d.queue_free()``, the remote scene tree looks like this.
|
|
|
|
|
|
-.. image:: img/nodes_and_scene_instances_remote_tree_with_sprite.png
|
|
|
|
|
|
+.. image:: img/nodes_and_scene_instances_remote_tree_with_sprite.webp
|
|
|
|
|
|
After the engine freed the node, the remote scene tree doesn't display the
|
|
After the engine freed the node, the remote scene tree doesn't display the
|
|
sprite anymore.
|
|
sprite anymore.
|
|
|
|
|
|
-.. image:: img/nodes_and_scene_instances_remote_tree_no_sprite.png
|
|
|
|
|
|
+.. image:: img/nodes_and_scene_instances_remote_tree_no_sprite.webp
|
|
|
|
|
|
You can alternatively call ``free()`` to immediately destroy the node. You
|
|
You can alternatively call ``free()`` to immediately destroy the node. You
|
|
should do this with care as any reference to it will instantly become ``null``.
|
|
should do this with care as any reference to it will instantly become ``null``.
|