Skip to content

Macros reference

IvanInventor edited this page Feb 1, 2024 · 16 revisions

All implemented macros

Class declaration

GCLASS

  • GCLASS (class_name, base_class_name) - register class
  • GVIRTUAL_CLASS (class_name, base_class_name) - register virtual class
  • GABSTRACT_CLASS (class_name, base_class_name) - register abstract class
  • GINTERNAL_CLASS (class_name, base_class_name) - register internal class

Macro that you need to put at the BOTTOM of your class declaration to register class and use other macros (Technically, if you're not using auto-generated setters/getters, you can put it anywhere).

class Example : public godot::Object {
	// ...
	
	GCLASS(Example, godot::Object);
};

Other macros

GINIT_LEVEL_***

  • GINIT_LEVEL_CORE ()
  • GINIT_LEVEL_SERVERS ()
  • GINIT_LEVEL_SCENE ()
  • GINIT_LEVEL_EDITOR ()

Register class at respective init level. Scene level by default. See godot::ModuleInitializationLevel.

class Example : public godot::Object {
	GINIT_LEVEL_CORE();

	// ...
};

GSIGNAL

  • GSIGNAL (signal_name, [[arg_type], arg_name], ...)

Register signal signal_name with N arguments of type arg_type or Variant.

class Example : public godot::Object {
	GSIGNAL(empty_signal);
	GSIGNAL(example_signal, int typed_arg, untyped_arg);

	// ...
};

GPROPERTY

  • GPROPERTY (setter, getter [, property_hint] [, property_hint_string])

Register property with setter/getter, and optional property hint and property string. Incorrect property types will raise compiler error.

Optional property_name expands to PROPERTY_HINT_<PROPERTY_NAME>. See PropertyHint enum in editor reference for possible type hints and for hint strings for each specific hint type.

Use Ref<ResourceType> properties for reference-counted properties.

Resource types are given PROPERTY_HINT_RESOURCE_TYPE with correct hint_string by default, override with none property_hint.

class Example : public godot::Object {
	GPROPERTY(set_float, get_float);
	float float_prop;

	// Slider from 0 to 1000 with step of 2
	GPROPERTY(set_int, get_int,
		range, "0,1000,2");
	int int_prop;
	
	// Will accept only AnimatedTexture
	GPROPERTY(set_anim, get_anim);
	Ref<AnimatedTexture> anim;
	
	// Will accept any texture
	GPROPERTY(set_anim_any, get_anim_any,
		none);
	Ref<Texture> anim_any;
};

GGROUP/GSUBGROUP

  • GGROUP (group_name)
  • GSUBGROUP (subgroup_name)

Set group/subgroup name for all following registered properties. Place empty macro to reset group.GGROUP() also resets previous subgroup

class Example : public godot::Object {
	GGROUP(group);
		GPROPERTY(set_float, get_float);
		// group_float_prop;
		float float_prop;
		
	GSUBGROUP(subgroup);
		GPROPERTY(set_int, get_int);
		// group_subgroup_int_prop;
		int int_prop;
	GGROUP();
	// vec_prop
	Vector2 vec_prop;
};

GBITFIELD

GBITFIELD ()

Register next enum as a bitfield. You have to set proper flags by yourself.

class Example : public godot::Object {
	// ...
public:
	GBITFIELD();
	enum Flags {
		FLAG_ONE = 1,
		FLAG_TWO = 2,
		FLAG_THREE = 4,
	};
};

GMETHOD

GMETHOD ()

Register next method. Useful when auto-registering methods is disabled, or when you need to register private method. Compiler error will be raised if method signature is not supported.

class Example : public godot::Object {
	// ...
	GMETHOD();
	void do_something();
};

GIGNORE

GIGNORE ()

Ignore next method/enum/constant declaration.

class Example : public godot::Object {
	// ...
	GIGNORE();
	int ignore_method();

	GIGNORE();
	enum ignore_enum {
		ONE,
		TWO,
	};

	GIGNORE();
	enum {
		IGNORE_CONSTANT = 42,
	};
};

GVARARG

GVARARG ([[arg_type] arg_name], ...)

Register vararg method. Takes argument names list with optional types similar to GSIGNAL.

Method signature must be void (const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error).

class Example : public godot::Object {
	// ...
	GVARARG(String named_arg, unnamed_arg);
	Variant varargs_func_example(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error);
};

GRPC

GRPC ([rpc_mode] [, transfer_mode] [, call_local], [, channel])

Configure as RPC method with respective parameters. See Node::rpc_config method and GDScript's @rpc in-editor reference.

You need to call _rpc_config() method on your node to enable configuration either in void _ready() override method or on NOTIFICATION_READY notification.

class Example : public godot::Object {
	// ...
	GRPC(authority, reliable, call_local);
	void rpc_example(int p_value);

	GRPC(any_peer, unreliable_ordered, call_remote, 42);
	void rpc_example2();
};

GBIND_METHODS_PREPEND

GBIND_METHODS_APPEND

GBIND_METHODS_PREPEND (code)

GBIND_METHODS_APPEND (code)

Prepends/appends code to _bind_methods body. You can use it for adding custom code (changes to generated .cpp files are discarded after respected header file change), or for runtime-heavy calls (file, database access).

class Example : public godot::Object {
	// ...
	// Make sure you include what you need to call
	// in your header
	GBIND_METHODS_PREPEND(
	String load_enum_names = get_enum_names();
	);

	GPROPERTY(set_enum, get_enum,
		enum, load_enum_names);
		//    ^^^^^^^^^^^^^^
	int enum_names;

	GCLASS(Example, godot::Object);
};

Result

void Example::_bind_methods() {

	String enum_names = get_enum_names();
	
	Method<&Example123::get_enum>::bind(D_METHOD("get_enum"));
	Method<&Example123::set_enum>::bind(D_METHOD("set_enum", "value"));


		ADD_PROPERTY(MakePropertyInfo<decltype(enum_names)>("enum_names", PROPERTY_HINT_ENUM, load_enum_names), "set_enum", "get_enum");
												//    ^^^^^^^^^^^^^^^
}

GRESOURCE_LOADER

GRESOURCE_SAVER

GRESOURCE_LOADER ()

GRESOURCE_SAVER ()

Registers custom resource loader/saver in engine. See documentation for correct class implementation. Macro handles loader/saver instance creation and registration in engine.

class ExampleLoader : public godot::ResourceFormatLoader {
	GRESOURCE_LOADER();
public:
	// ...
	// Implement needed virtual methods here
	// ...

	GCLASS(ExampleLoader, godot::ResourceFormatLoader);
};

GEDITOR_PLUGIN

GEDITOR_PLUGIN ()

Registers editor plugin. See documentation for correct plugin implementation. Assumes GINIT_LEVEL_EDITOR

class ExamplePlugin : public godot::EditorPlugin {
	GEDITOR_PLUGIN();

	// ...
	// Implement needed virtual methods here
	// ...

	GCLASS(ExamplePlugin, godot::EditorPlugin);
};

GSINGLETON

GSINGLETON ()

Registers singleton in engine. You must implement get_singleton method to return pointer to singleton instance. Macro handles instance creation and deletion. See example for usual singleton creation.

// .hpp file
class ExampleSingleton : public godot::Node {
	GSINGLETON();

	static ExampleSingleton* singleton;
	
public:
	ExampleSingleton();

	static ExampleSingleton* get_singleton();

	GCLASS(ExampleSingleton, godot::Node);
};
// .cpp file
ExampleSingleton* ExampleSingleton::singleton;

ExampleSingleton::ExampleSingleton() {
	singleton = this;
}
ExampleSingleton* ExampleSingletonget_singleton() {
	return singleton;
}
Clone this wiki locally