Derive Macro gdnative_derive::NativeClass[][src]

#[derive(NativeClass)]
{
    // Attributes available to this derive:
    #[inherit]
    #[export]
    #[opt]
    #[user_data]
    #[property]
    #[register_with]
    #[no_constructor]
}

Makes it possible to use a type as a NativeScript.

Required attributes

The following attributes are required on the type deriving NativeClass:

#[inherit(gdnative::api::BaseClass)]

Sets gdnative::api::BaseClass as the base class for the script. This must be a type from the generated Godot API (that implements GodotObject). All owner arguments of exported methods must be references (TRef, Ref, or &) to this type.

Inheritance from other scripts, either in Rust or other languages, is not supported.

Optional type attributes

Behavior of the derive macro can be customized using attribute on the type:

#[user_data(gdnative::user_data::SomeWrapper<Self>)]

Use the given type as the user-data wrapper. See the module-level docs on gdnative::user_data for more information.

#[register_with(path::to::function)]

Use a custom function to register signals, properties or methods, in addition to the one generated by #[methods]:

#[derive(NativeClass)]
#[inherit(Reference)]
#[register_with(my_register_function)]
struct Foo;

fn my_register_function(builder: &ClassBuilder<Foo>) {
    builder.add_signal(Signal { name: "foo", args: &[] });
    builder.add_property::<f32>("bar")
        .with_getter(|_, _| 42.0)
        .with_hint(FloatHint::Range(RangeHint::new(0.0, 100.0)))
        .done();
}

#[no_constructor]

Indicates that this type has no zero-argument constructor. Instances of such scripts can only be created from Rust using Instance::emplace. Instance::new or ScriptName.new from GDScript will result in panics at runtime.

See documentation on Instance::emplace for an example on how this can be used.

Optional field attributes

#[property]

Convenience attribute to register a field as a property. Possible arguments for the attribute are:

  • path = "my_category/my_property_name"

Puts the property under the my_category category and renames it to my_property_name in the inspector and for GDScript.

  • default = 42.0

Sets the default value in the inspector for this property. The setter is not guaranteed to be called by the engine with the value.

  • before_get / after_get / before_set / after_set = "Self::hook_method"

Call hook methods with self and owner before and/or after the generated property accessors.

  • no_editor

Hides the property from the editor. Does not prevent it from being sent over network or saved in storage.