0 votes

I am learning Gscript, I have a very basic background of programming in python, but I run into a problem that I can't understand.

I have a bool variable in other script and a delta process in another script which calls this Bool

The problem is that the value of the variable never changes.

The script delta is these:

onready var change = get_tree().get_root().get_node("Main/HUD").get("status")

func _process(delta):

if (change == false):
    print (change)
if (change == true):
    print (change)

And the bool script is this

var status = false

func _on_NormalButton_pressed():
var status = true
$NormalButton.hide()

The buttons hide but status value never changes to true in Delta, what is wrong with my approach?

in Engine by (26 points)

2 Answers

+1 vote
Best answer

The variable change contains a copy of the value of status, not a reference! If you change status later, the value of change won't change. This should work:

func _process(delta):
    var change = get_node("/root/Main/HUD").status
    print(change)

If you want to avoid polling for the value each frame, you can emit a signal when the value of status changes and connect your printing function to it.

by (10,327 points)
selected by
+1 vote

Hello, I can't verify it now in godot but I think the error is that you are redefining the status variable with the word "var" therefore the original variable never changes, try this:

var status = false

func _on_NormalButton_pressed():
status = true
$NormalButton.hide()
by (2,060 points)

Thank you very much, you're right this could end up being redundant, I just changed it

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.