• Godot HelpProgramming
  • Is there a way to expose C++ enums into gdscript? Or more precisely how is it done in the engine?

I've already googled and searched through code, but I can't find any clear example

Enums don't exist post-compile, they just get converted to numbers. They are effectively just a convenience for the programmer and are an abstract way of representing integers.

When you make a DLL/SO/DYLIB you are compiling your C++ code down into the dynamic library that you then connect to Godot. Your enums no longer exist at that point. You can make your own enums on Godot's side that mirror your enum values in C++, I suppose.

In the engine code, for example the Button node, you have this in the header file:

class Button : public BaseButton {
	GDCLASS(Button, BaseButton);
public:
	enum TextAlign {
		ALIGN_LEFT,
		ALIGN_CENTER,
		ALIGN_RIGHT
	};
	//...
	static void _bind_methods();
	//...
};

// This is needed for some C++ casting stuff
VARIANT_ENUM_CAST(Button::TextAlign);

And then in the cpp file, TextAlign is bound using the following macros, inside the _bind_methods function:

	BIND_ENUM_CONSTANT(ALIGN_LEFT);
	BIND_ENUM_CONSTANT(ALIGN_CENTER);
	BIND_ENUM_CONSTANT(ALIGN_RIGHT);

You can have a look in these files: https://github.com/godotengine/godot/blob/master/scene/gui/button.h https://github.com/godotengine/godot/blob/master/scene/gui/button.cpp

You can create and expose c++ enums in modules but not gd-native. The above examples of BIND_ENUM_CONSTANT come from modules. Unless I am terribly mistaken, again after looking, these macros are unavailable in gd-native because it operates a bit differently than classes built into the engine itself.

I have switched to building modules.

In general I am porting c++ classes for use in godot. I had believed gd-native would be a convenient way, but without being able to express and export enums to gd-native that pathway is not useful.

3 years later