This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+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,286 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 (101 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 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.