+1 vote

I'm not sure what to hunt for on this, but I was wondering how many times a constant will be assigned to memory when you define one inside a class.

Say you did this:

class MyObj:
    const DATA = 123

func _ready():
    var arr = []
    for i in range(100): arr.append(MyObj.new())

Will Godot store 100 copies of this constant in memory?

in Engine by (5,212 points)

When I fill an array with objects I can access to the objects using array index so I think array stores pointers and not a copy of the objects.
You can test if it's works like this even on simple types like integers.

1 Answer

+4 votes
Best answer

Looking at the GDScript parser, it appears that constant values are assigned at parse time, so I'd imagine there's no memory overhead or multiple instantiation in classes.

by (97 points)
selected by

It also looks like the compiler bears that out—whenever a parsed constant node is encountered in the parser's output, it sets the parsed value in a constant map on the codegen object.

Thanks so much. I got lost in the Tokenizer code.

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 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 webmaster@godotengine.org with your username.