0 votes

I have a player in the main scene that adds a fireball along with the player to the main scene
however I want this in the fireball code to change the value of the child node from the parent player

i found a solution

get_parent (). get_node ("KinematicBody2D / CanvasLayer / Control / GMP"). value + = 10 # get_parent () == level, KinematicBody2D == player

but is it possible not to use an absolute path?

in Engine by (29 points)
edited by

1 Answer

0 votes

Programminst avoid using absolute paths, because it makes nodes stiffly dependent on their scene tree allocation. If You were to decide some day, that You want player to be child of a different node, or You would want to add node inbetween nodes - You will have to change all of the absolute paths in whole your code, because they will start to crash your project.

Generally You want to call and change values of other nodes at short distances ( parent - child ), but when you want to pass information along whooole scene tree, like in your example : use signals !!

by (2,434 points)

translator error, I asked you can use something else instead of the absolute path

You can and You should use signals for that. Read documentation about them. The way it works is your Fireball emits a signal with some information when you want ( for example if enemy is hit emit signal how many hit points it took form enemy ) and other nodes can listen to that signal. You can imagine it as radiostation - You make anouncement in radio, that you want to hire someone, and people who are intereseted will hear this and come to you.
normally listening to signal requires nodes to be connected to each other, and this also requires absolute path. However You don't need any paths if You use Autoload script as signals mediator ! ( read about it in documentation )

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 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 webmaster@godotengine.org with your username.