Can't increment a variable from another script

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

I have 2 scripts, from 2 different scenes, and I’m trying to change a variable in one of them, through the other.

In the first script, I have the variable

scoreMultiplier = 1

And in the second,

onready var score = get_node("/root/world/score").scoreMultiplier

func _physics_process(delta):
	if position.y <= -10:
		score += 0.05

But the scoreMultiplier variable doesn’t seem to increment. The second script IS getting the variable, tho. if i print(score) before queue_free() it prints 1 to the console.

Any thoughts?

:bust_in_silhouette: Reply From: njamster

You’re getting a copy of the value of scoreMultiplier not a reference here:

onready var score = get_node("/root/world/score").scoreMultiplier

Consequentially you only increase the value of that copy, not the original value.

However, the following should work (as more complex objects are indeed passed by reference as opposed to a simple integer or float that is passed by value):

onready var score_node = get_node("/root/world/score")

func _physics_process(delta):
    if position.y <= -10:
        score_node.scoreMultiplier += 0.05

Yesss! It worked flawlessly :slight_smile:
Thank you sir!

andreluizgollo | 2020-05-11 19:40