-
Notifications
You must be signed in to change notification settings - Fork 0
Macros reference
-
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 in your class declaration to register class and use other macros (replaces GDCLASS
and _bind_methods
method).
class Example : public godot::Object {
GCLASS(Example, godot::Object);
// ...
};
-
GINIT_LEVEL_CORE
()
-
GINIT_LEVEL_SERVERS
()
-
GINIT_LEVEL_SCENE
()
-
GINIT_LEVEL_EDITOR
()
Register class at corresponding init level. Scene level by default. See godot::ModuleInitializationLevel
.
class Example : public godot::Object {
GINIT_LEVEL_CORE();
// ...
};
-
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
(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.
If provided setter
/getter
method doesn't exist, it will be generated automatically by the same name and available from C++.
Optional property_hint
expands to PROPERTY_HINT_<PROPERTY_HINT>
. 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
(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
()
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
()
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
()
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
([[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
([rpc_mode] [, transfer_mode] [, call_local], [, channel])
Configure as RPC method with corresponding 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
(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 corresponding 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 load_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
()
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
()
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
()
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;
}
GSTATIC_MEMBER
(type, name [, init])
Defines static member name
of type
type, which initializes by type(init)
expression and destructs, if needed, at corresponding init level. Macro also handles creating member definition in generated source file (no need for type Class::name = ...;
line in your .cpp file)
class Example : public godot::Node {
GSTATIC_MEMBER(godot::String, static_str, "123456");
GCLASS(Example, godot::Node);
};
SNAME
(string_literal)
Similar to Godot engine's SNAME macro idea or GDScript's &"string_name"
syntax, it creates static instance of StringName and returns reference to it. It guarantees that StringName
exists before calling this lambda, but not that StringName
s with same string literal are the same object.
#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/input_event.hpp>
class Example : public godot::Node {
void _input(const godot::Ref<godot::InputEvent>& ev) override {
if(godot::Input::get_singleton()->is_action_just_pressed(SNAME("my_action")))
godot::UtilityFunctions::print("Action pressed");
}
GCLASS(Example, godot::Node);
};