0 votes

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
        queue_free()

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?
Thanks!

in Engine by (42 points)

1 Answer

+3 votes
Best answer

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
        queue_free()
by (10,491 points)
selected by

Yesss! It worked flawlessly :)
Thank you sir!

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.