Struct gdnative::prelude::ClassBuilder
source · 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,
impl<C> ClassBuilder<C>where
C: NativeClass,
sourcepub fn method<F, 'a>(
&'a self,
name: &'a str,
method: F
) -> MethodBuilder<'a, C, F>where
F: Method<C>,
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")
}
}
sourcepub fn property<T, 'a>(
&'a self,
name: &'a str
) -> PropertyBuilder<'a, C, T, InvalidSetter<'a>, InvalidGetter<'a>>where
T: Export,
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();
}
}
sourcepub fn signal(&self, name: &str) -> SignalBuilder<'_, C>
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();
}
}
sourcepub fn mixin<M>(&self)where
M: Mixin<C>,
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>();
}