Is constant variable inside class a singleton?

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

I have class, I have constant reference to another script in it.

I wonder if for every instance of my class there is another “copy” of reference to same object?
What I care is memory - if for egzample creating 10’000 objects with 20’000 const references will result in 10’000 new objects and 20’000 shared references or 10’000 new objects and 200’000’000 new references? (that numbers are purely theoritical)

what I do not care is answers that ‘it will have same behaviour’ or that ‘this ammounts of memory doesn’t matter’

we do not have static var, so I can’t use it here and we don’t have include so I need to import another script as object, so thats only option I can think of…

For my question first option (shared references) would look very reasonable, so I wonder if it is already an engine feature, or if I’m missing some case when first option wouldn’t be reasonable

:bust_in_silhouette: Reply From: Zylann

const members are stored in the script itself, not the instances of that script. It will remain set as long as something in the game has a reference to it.

For example:

# In
const OtherClass = preload("")

If you make more instances of, the OtherClass constant reference will not occupy more memory.
Also, the preload will run once as soon as is loaded for the first time, and will not run again, unless all references to are removed.