+1 vote

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

in Engine by (23 points)
edited by

1 Answer

+2 votes

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 my_script.gd
const OtherClass = preload("other_class.gd")

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

by (29,120 points)
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 Frequently asked questions and 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 [email protected] with your username.