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: NativeClass> ClassBuilder<C>
impl<C: NativeClass> ClassBuilder<C>
Sourcepub fn method<'a, F: Method<C>>(
&'a self,
name: &'a str,
method: F,
) -> MethodBuilder<'a, C, F>
pub fn method<'a, F: Method<C>>( &'a self, name: &'a str, method: F, ) -> MethodBuilder<'a, C, F>
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<'a, T>(&'a self, name: &'a str) -> PropertyBuilder<'a, C, T>where
T: Export,
pub fn property<'a, T>(&'a self, name: &'a str) -> PropertyBuilder<'a, C, T>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: Mixin<C>>(&self)
pub fn mixin<M: Mixin<C>>(&self)
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§
Auto Trait Implementations§
impl<C> !Freeze for ClassBuilder<C>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more