How to get an Array of all const's of an object/class?

Godot Version

4.3

Question

How to get an Array of all const’s of an object/class?

According to the documentation for Object, get_property_list() should return an array of properties. And get() is supposed to work for a property, and is returning the expected result if passed the name of a const. But get_property_list() is lacking those same consts.

Any idea how to get an Array of ['a','b','c'] from this object?

class MyClass:
	const a = '1'
	const b = '2'
	const c = '3'

func _ready() -> void:
	var obj := MyClass.new()
	var l:= obj.get_property_list() # returns dicts, but we just need the names.
	var l2 := []
	for i in l:
		l2.append(i['name'])
	print(l2) # ["RefCounted", "script", "Built-in script"]
	print(obj.get('a')) # 1
	# ????? print ['a','b','c']

if a in obj: array.append['a']
get_property_list() is for mostly debugging things and works during debug remote, and not suits to filter things.
so its enough to check if property exist just by if &'property_name' in Object:

1 Like

I’m hoping for a way to scan an object and return all its consts, whose names aren’t known at runtime.

Similar methods like get_method_list() and get_meta_list() exists for other data, so I was hoping consts would be included in at least one of these.

I agree about the debugging - the goal would be to do some validation checks on any consts found.

constants always known. Even during static func _static_init().

constants are tightly tied to script from where they belong so its better to check using is operator or typeof()

if obj is MyClass:
	do_something()

I am working with consts containing specially-formatted Array data which I wish to validate, with each Level having its own set of such constants.

It would be ideal to be able to scan for them and validate them. Doing an if/elif for every single Level would not be ideal.

Is this possible or not?

Script.get_script_constant_map() use it

var test=MyClass.new().get_script().get_script_constant_map()

That’s perfect! Thank you so much!!

{ "a": "1", "b": "2", "c": "3" }