0 votes

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?

in Engine by (380 points)

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

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

2 Answers

0 votes
Best answer

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

by (380 points)
0 votes

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
by (137 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.