How to change the value of a bool?

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

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?

:bust_in_silhouette: Reply From: estebanmolca

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

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

Neosss | 2020-04-24 11:37

:bust_in_silhouette: Reply From: njamster

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.