Modifying global variables

Hi. I’m thinking of setting up an autoload in my app but have had trouble previously with changes to the variables not being persistent. Basically they were affecting the local copy not the original autoload and were therefore not persisting through scene changes. Wanting to know how to ensure the original global gets modified and persists rather than just a copy? I’m going to set up an empty dictionary that will track character selection for the players and want it to persist between scenes. Optionally I can use a class I have set up already but would still like to know the answer to modifying a global. All the scenes are control scenes.

What’s a “local copy”? An autoload doesn’t produce any copies. It’s just a persistent, globally accessible node running a script.

Last time it wasn’t keeping a persistent array. It would just reset to the original values on scene changes

No, you just didn’t fully understand what was happening. Your last problem had nothing to do with autoload persistency, although you wrongly interpreted it did. Re-read again my posts in that thread.

Autoloads are 100% persistent.

I wouldn’t say 100% as others have also had persistence issues with them. I think I’ll just stick to using the autoload for my constants and statics for my shared variables.

There are no “persistency issues” with autoloads. You were trying to store state that is not persistent. If you pour water into a cup and that water evaporates over night, you can’t say it’s because the cup is not persistent.

As I said others have had issues also. I also never said that my last attempt was the same one I previously asked about on this forum. I also ticked solved in my last post in this thread so don’t feel any need to spend more time on replying.

I’ll experiment further in my next project.

Only very basic types are copied, float, int, bool, String are all copied. Any type with a .duplicate() are referenced, Object, Array, Dictionary are all referenced by assignment.

var my_int: int = 123

var copied_int: int = my_int
copied_int = 456

print(my_int) # 123
print(copied_int) # 456
# the copied int does not change the original

####
var my_array: Array = [123]

var ref_array: Array = my_array
ref_array.append(789)
ref_array[0] = 456

print(my_array) # [456, 789]
print(ref_array) # [456, 789]
# the array is referenced, changes to ref_array affect the original variable.

This behavior is consistent with or without an autoload

1 Like

There are no technical issues with autoload persistency on the engine side. The only “issues” anyone might have had is due to not fully understanding how they work and therefore misusing them.

1 Like