How can I get a node from an instanced scene by name?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MaaaxiKing

My SceneTree:

Game (Node)

Player (KinematicBody2D) (instanced, script attached) (has an AnimatedSprite as a child with animation_finished() connected to the player script)
Ball (RigidBody2D) (instanced, script attached)

In the player script I tried:

func _on_AnimatedSprite_animation_finished():
  1. get_tree().get_root().get_node("Ball").my_function()
  2. get_tree().get_node("Ball").my_function()
  3. get_parent().get_node("Ball").my_function().

How can I call a function on the Ball or change some of its properties?

The thing is I don’t know, that when you instance the ball, what is it a child of?? This information is kinda nececarry.

Czselu349 | 2020-06-24 20:30

I instanced the scene even before the game has started. The Ball is child of Game.

MaaaxiKing | 2020-06-24 20:38

:bust_in_silhouette: Reply From: Czselu349

I think, that the 1st option should work just fine.
I f you want to change a property of another node you can do it like this:

get_tree().get_root().get_node("name_of_the_node").property = value
:bust_in_silhouette: Reply From: MaaaxiKing

I just changed to get_node("/root/Game/Ball").my_function() and now it works.