Global variable

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

I have a variable “example” on autoload scene “scene”
how to synchronize it in another scene so as not to constantly use “scene.example”

for example, I use

var examp = scene.example

and I want to when changing “examp” changed and “scene.example”

:bust_in_silhouette: Reply From: wombatstampede

You could define setget functions for your variable:

Inside those functions you return or set the value of the global variable.

If you are adressing the contents of complex data types (dictionaries, objects, arrays) you can also assign that variable to a local var which then (always) receives a kind of pointer.

So if you define a dictionary in scene:

var my_settings = {"audio_volume":0.5, "cheat": false}

Then you could copy that dictionary “pointer” to a local var and use that instead:

var glb_settings = scene.my_settings

glb_settings["audio_volume"] = 0.75
print("cheat is: "+str(glb_settings["cheat"]))

Although all this is not very “transparent” and may be difficult to understand if you (or someone else) looks at the code at a later time. So I opt for just referencing the global singleton. (i.e. scene.example).