pub struct ClassBuilder<C> { /* private fields */ }
Expand description

Allows registration of exported properties, methods and signals.

See member functions of this class for usage examples.

Implementations§

Returns a MethodBuilder which can be used to add a method to the class being registered.

Examples

Basic usage:

use gdnative::prelude::*;
use gdnative::export::{RpcMode, Varargs};

#[derive(NativeClass)]
#[register_with(Self::my_register)]
#[no_constructor]
struct MyType {}

// Note: no #[methods] required
impl MyType {
    fn my_method(&self) -> i64 { 42 }

    fn my_register(builder: &ClassBuilder<MyType>) {
        builder
            .method("my_method", MyMethod)
            .with_rpc_mode(RpcMode::RemoteSync)
            .done();
    }
}

// Now, wrap the method (this can do anything and does not need to actually call a method)
struct MyMethod;
impl Method<MyType> for MyMethod {
    fn call(&self, this: TInstance<'_, MyType>, _args: Varargs<'_>) -> Variant {
        this.map(|obj: &MyType, _| {
            let result = obj.my_method();
            Variant::new(result)
        }).expect("method call succeeds")
    }
}

Returns a PropertyBuilder which can be used to add a property to the class being registered.

Examples

Basic usage:

use gdnative::prelude::*;

#[derive(NativeClass)]
#[inherit(Node)]
#[register_with(Self::my_register)]
#[no_constructor]
struct MyType {
    foo: i32,
}

// Note: no #[methods] required
impl MyType {
    pub fn get_foo(&self, _owner: TRef<Node>) -> i32 { self.foo }
    pub fn set_foo(&mut self, _owner: TRef<Node>, val: i32) { self.foo = val; }

    fn my_register(builder: &ClassBuilder<MyType>) {
        builder
            .property("foo")
            .with_default(5)
            .with_hint((-10..=30).into())
            .with_getter(MyType::get_foo)
            .with_setter(MyType::set_foo)
            .done();
    }
}

Returns a SignalBuilder which can be used to add a signal to the class being registered.

Examples

Basic usage:

use gdnative::prelude::*;

#[derive(NativeClass)]
#[inherit(Node)]
#[register_with(Self::my_register)]
#[no_constructor]
struct MyType {}

// Note: no #[methods] required
impl MyType {
    fn my_register(builder: &ClassBuilder<MyType>) {
        // Add signal without parameters
        builder
            .signal("jumped")
            .done();

        // Add another signal with 1 parameter (untyped)
        builder
            .signal("fired")
            .with_param_untyped("weapon_type")
            .done();

        // Add third signal with int + String parameters, the latter with a default value "Kerosene"
        builder
            .signal("used_jetpack")
            .with_param("fuel_spent", VariantType::I64)
            .with_param_default("fuel_type", Variant::new("Kerosene"))
            .done();
    }
}

Add a mixin to the class being registered.

Examples
use gdnative::prelude::*;

#[derive(NativeClass)]
#[inherit(Node)]
#[register_with(my_register)]
#[no_constructor]
struct MyType {}

// This creates a opaque type `MyMixin` in the current scope that implements
// the `Mixin` trait. Mixin types have no public interface or stable layout.
#[methods(mixin = "MyMixin")]
impl MyType {
    #[method]
    fn my_method(&self) -> i64 { 42 }
}

fn my_register(builder: &ClassBuilder<MyType>) {
    builder.mixin::<MyMixin>();
}

Trait Implementations§

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.