Can't change variable of parent node with timer

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

I am attempting to use a Timer to change a variable within the Timer’s parent node upon timeout.

func _on_DashTimer_timeout():
get_node(“root/Game/Level_1/Player”).ACCELERATION = 10
get_node(“root/Game/Level_1/Player”).MAX_SPEED = 50
get_node(“root/Game/Level_1/Player”).velocity = get_node(“root/Game/Level_1/Player”).current_velocity

However, when I try to run it, it comes up with the error “Invalid set index ‘ACCELERATION’ (on base: null instance) with value of type ‘int’”

Anyone know how to fix this?

:bust_in_silhouette: Reply From: deaton64

A slight educated guess. The get node command isn’t finding your Player node.

Put a print statement and if it’s null then that’s what it is:

print (get_node("root/Game/Level1/Player"))

What I do is use a Global script to set the player node in the player ready function:

Global.player = self

then you can access the variables in you timer function like this:

Global.player.ACCELERATION = 10

You will have to make sure the player node sets its node value before accessing it with other scripts. Either spawn your player first or check that Global.player is set.

:bust_in_silhouette: Reply From: skysphr

If you’re trying to change a variable within the timer’s parent, you can do it without accessing the absolute path to the node; Assuming the timer’s parent is the player object itself, the function could be written as:

func _on_DashTimer_timeout():
    MAXSPEED = 50
    velocity = current_velocity