A general question about how GDScript handles Constants.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By avencherus
:warning: Old Version Published before Godot 3 was released.

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?

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.

DriNeo | 2017-01-06 17:35

:bust_in_silhouette: Reply From: jslater89

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.

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.

jslater89 | 2017-01-06 19:30

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

avencherus | 2017-01-07 02:03