How can I change a bool that is inside the _process() function from false to true.

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

It appears that when I try to use a member function to change the value of a bool that is a member variable, the value of the bool inside of the _process() function does not change.

I put together a brief demo project to illustrate the problem that I am having.
The first script, “”, is attached to the root node which is a Spatial node.
The second script, “Flip Bool”, is attached to a button. When the button is pushed, a member variable inside “” is called that is supposed to change the value of the bool, testBool, to true. Then, an “if” statement inside the _process function is supposed to start printing that the bool is true.

extends Spatial

var testBool = false

func _ready():
	print("Scene_1 script, at _ready(), testBool = ", testBool)

func _process(delta):
	if(self.testBool == true):
		print("Scene_1 script, at _process, testBool = ", testBool)

func flipBool():
	self.testBool = true
	print("Scene_1 script, flipBool() called, testBool = ", testBool)

Flip Button

extends Button

onready var root_script = load("res://").new()

func _pressed():
	print("Flip Bool Button script, button pressed, testBool = ", root_script.testBool)
	print("Flip Bool Button script, flipBool() called")

I would expect that when the button is pushed, the testBool becomes true and the “if” statement in the _process function starts printing that this is the case. Instead, when the button is pressed the following is output:

 ** Debug Process Started ** 
OpenGL ES 3.0 Renderer: Quadro M2200/PCIe/SSE2
Scene_1 script, at _ready(), testBool = False
Flip Bool Button script, button pressed, testBool = False
Scene_1 script, flipBool() called, testBool = True
Flip Bool Button script, flipBool() called
** Debug Process Stopped **

As you can see, the flipBool() function is called, and if the value of testBool is printed, it will show true. However, the if statement in the _process function never shows that testBool is true.

Why is this happening? Am I doing something wrong?

:bust_in_silhouette: Reply From: Zylann
onready var root_script = load("res://").new()

This line is wrong.
It creates a new instance, it does not get the one already present in the scene. So you are setting a bool on an instance that actually doesn’t run because it’s not the one in the tree.

You should do this instead:

onready var root_script = get_node("Relative/Path/To/Scene_1")

I can’t tell you the path though, it depends on how you setup your nodes.

Thanks for the answer!
I was starting to consider that and go down that road.
The name of the node at the top of the scene tree and containing the bool is “RootNode”. So I changed the line to:

onready var root_script = get_node("/root/RootNode")

Now it works the way I expected.
The following also works:

onready var root_script = $"/root/RootNode"

Stopdot | 2019-09-11 18:52