How can i change a variable's value from another script without autoloading it/its scene?

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

I have the Scene “Globals” and the rootNode’s name is “Globals” too. This rootNode has a script named “Globals”. A child of the rootNode has the name “KeyBoard” and this node has the script “KeyBoard”.
I want to set a value of the GlobalsScript from the KeyBoardScript but I don’t want to autoload anything of these scenes or scripts because then there are other faults, e.g. if I autoloaded the GlobalsScript, I could change the value of any of its variables from anywhere but then I couldn’t do anything with the nodes of the GlobalsScene from the GlobalsScript. I tried that out. And in no way I want the two scripts to be one single script. What do I have to do to get it working?

:bust_in_silhouette: Reply From: njamster

While I didn’t understand what your problem with AutoLoads is about, the only way to change a variable is to get the node-instance the script is attached to.

So for your example (and a variable called “test”):

get_parent().test = 123

or

get_node("../Globals").test = 123

or

get_node("/root/Globals").test = 123

My problem with autoloads is that I see the things of the autoloaded scene the whole time the game is running but I don’t want it to be so.

MaaaxiKing | 2020-04-16 08:14

Yesterday, I tried get_parent().my_variable = "something" but it didn’t work, respectively I thought it wouldn’t work. Now I found what was wrong. There were errorlines and at first I ignored them. It didn’t suggest my_variable but when I commented out the errorlines it worked. Anyway, thank you very much.

MaaaxiKing | 2020-04-16 08:19

My problem with autoloads is that I see the things of the autoloaded scene the whole time the game is runningbut I don’t want it to be so.

You can self.hide() the autoloaded scene on _ready and self.show() it when you need it. The script will still be processed, even if the scene is hidden.

njamster | 2020-04-16 11:21