What is the difference between a Class made with 'class_name' and an inner class made with 'class'?

Godot Version

4.6.1.stable

Question

Why is the reference to a ‘class_name’ class not suitable for a constant expression, but an inline ‘class’ is?

I have a ThemeManager that manages custom Themes.
When I add a newly made Theme for the ThemeManager to manage, I want to only have to add it in one place to prevent the amount of mistakes that are possible (single point of entry).

I have created two themes that both extend an @abstract base class.

# ThemeDefaultDark.gd

extends		BareTheme
class_name	ThemeDefaultDark

# ...

and in ThemeManager.gd I have added an inline class.

#ThemeManager.gd

# ...

class	ThemeDefaultLight:
	extends BareTheme

	# ...

The amount or types of themes I have does not change at runtime therefore I have created a constant array as the single point of entry to reference them in.

#ThemeManager.gd

const	THEMES	:	Array	=	[
	ThemeDefaultLight,			# No problems with inline class
	ThemeDefaultDark,			# Assigned value for constant "THEMES" isn't a constant expression.
	preload("res://ThemeDefaultDark.gd"),	# No problems with preload
]

Why does this behave differently?

I’ve ran into this too, supposedly it’s just a bug that a classes created with class_name aren’t considered const, but it’s been like that for years, don’t know if it’ll be fixed any time soon.

1 Like