0 votes

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, "Scene1.gd", is attached to the root node which is a Spatial node.
The second script, "Flip Bool Button.gd", is attached to a button. When the button is pushed, a member variable inside "Scene
1.gd" 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.

Scene_1.gd:

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 Bool.gd:

extends Button

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

func _pressed():
    print("Flip Bool Button script, button pressed, testBool = ", root_script.testBool)
    root_script.flipBool()
    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?
Thanks,

in Engine by (17 points)

1 Answer

+1 vote
Best answer
onready var root_script = load("res://Scene_1.gd").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.

by (27,902 points)
selected by

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"
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.