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§

source§

impl<C> ClassBuilder<C>where
    C: NativeClass,

source

pub fn method<F, 'a>(
    &'a self,
    name: &'a str,
    method: F
) -> MethodBuilder<'a, C, F>where
    F: Method<C>,

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")
    }
}
source

pub fn property<T, 'a>(
    &'a self,
    name: &'a str
) -> PropertyBuilder<'a, C, T, InvalidSetter<'a>, InvalidGetter<'a>>where
    T: Export,

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();
    }
}
source

pub fn signal(&self, name: &str) -> SignalBuilder<'_, C>

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();
    }
}
source

pub fn mixin<M>(&self)where
    M: Mixin<C>,

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§

source§

impl<C> Debug for ClassBuilder<C>where
    C: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<C> !RefUnwindSafe for ClassBuilder<C>

§

impl<C> !Send for ClassBuilder<C>

§

impl<C> !Sync for ClassBuilder<C>

§

impl<C> Unpin for ClassBuilder<C>where
    C: Unpin,

§

impl<C> UnwindSafe for ClassBuilder<C>where
    C: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.