1
- //! Types and functionalities to declare and initialize gdnative classes.
1
+ //! Low-level API to register and export GDNative classes, methods and properties .
2
2
//!
3
- //! ## API endpoints
3
+ //! ## Init and exit hooks
4
4
//!
5
5
//! Three endpoints are automatically invoked by the engine during startup and shutdown:
6
6
//!
7
- //! - [`godot_gdnative_init`](macro.godot_gdnative_init.html) ,
8
- //! - [`godot_nativescript_init`](macro.godot_nativescript_init.html) ,
9
- //! - [`godot_gdnative_terminate`](macro.godot_gdnative_terminate.html) ,
7
+ //! - [`godot_gdnative_init`],
8
+ //! - [`godot_nativescript_init`],
9
+ //! - [`godot_gdnative_terminate`],
10
10
//!
11
11
//! All three must be present. To quickly define all three endpoints using the default names,
12
- //! use [`godot_init`](macro.godot_init.html) .
12
+ //! use [`godot_init`].
13
13
//!
14
14
//! ## Registering script classes
15
15
//!
16
- //! To register script classes, call `InitHandle::add_class` or `InitHandle::add_tool_class`
17
- //! in your `godot_nativescript_init` or `godot_init` callback:
16
+ //! [`InitHandle`] is the registry of all your exported symbols.
17
+ //! To register script classes, call [`InitHandle::add_class`] or [`InitHandle::add_tool_class`]
18
+ //! in your [`godot_nativescript_init`] or [`godot_init`] callback:
18
19
//!
19
20
//! ```ignore
20
- //! // - snip -
21
+ //! use gdnative::prelude::*;
21
22
//!
22
- //! fn init(handle: gdnative::init:: InitHandle) {
23
+ //! fn init(handle: InitHandle) {
23
24
//! handle.add_class::<HelloWorld>();
24
25
//! }
25
26
//!
26
27
//! godot_init!(init);
27
- //!
28
- //! // - snip -
29
28
//! ```
30
29
//!
31
30
//! For full examples, see [`examples`](https://github.com/godot-rust/godot-rust/tree/master/examples)
@@ -291,15 +290,39 @@ impl<C: NativeClass> ClassBuilder<C> {
291
290
/// # Examples
292
291
///
293
292
/// Basic usage:
293
+ /// ```
294
+ /// use gdnative::prelude::*;
295
+ /// use gdnative::nativescript::init::{RpcMode, Varargs};
296
+ ///
297
+ /// #[derive(NativeClass)]
298
+ /// #[register_with(Self::my_register)]
299
+ /// #[no_constructor]
300
+ /// struct MyType {}
294
301
///
295
- /// ```ignore
296
- /// // `Bar` is a stateful method implementing `Method<C>`
302
+ /// // Note: no #[methods] required
303
+ /// impl MyType {
304
+ /// fn my_method(&self) -> i64 { 42 }
297
305
///
298
- /// builder
299
- /// .build_method("foo", Bar { baz: 42 })
300
- /// .with_rpc_mode(RpcMode::RemoteSync)
301
- /// .done();
306
+ /// fn my_register(builder: &ClassBuilder<MyType>) {
307
+ /// builder
308
+ /// .build_method("my_method", MyMethod)
309
+ /// .with_rpc_mode(RpcMode::RemoteSync)
310
+ /// .done();
311
+ /// }
312
+ /// }
313
+ ///
314
+ /// // Now, wrap the method (this can do anything and does not need to actually call a method)
315
+ /// struct MyMethod;
316
+ /// impl Method<MyType> for MyMethod {
317
+ /// fn call(&self, this: RefInstance<'_, MyType, Shared>, _args: Varargs<'_>) -> Variant {
318
+ /// this.map(|obj: &MyType, _| {
319
+ /// let result = obj.my_method();
320
+ /// Variant::from_i64(result)
321
+ /// }).expect("method call succeeds")
322
+ /// }
323
+ /// }
302
324
/// ```
325
+ ///
303
326
#[ inline]
304
327
pub fn build_method < ' a , F : Method < C > > (
305
328
& ' a self ,
@@ -316,14 +339,32 @@ impl<C: NativeClass> ClassBuilder<C> {
316
339
///
317
340
/// Basic usage:
318
341
///
319
- /// ```ignore
320
- /// builder
321
- /// .add_property("foo")
322
- /// .with_default(0.0)
323
- /// .with_hint((-10.0..=30.0).into())
324
- /// .with_getter(MyType::get_foo)
325
- /// .with_setter(MyType::set_foo)
326
- /// .done();
342
+ /// ```
343
+ /// use gdnative::prelude::*;
344
+ ///
345
+ /// #[derive(NativeClass)]
346
+ /// #[inherit(Node)]
347
+ /// #[register_with(Self::my_register)]
348
+ /// #[no_constructor]
349
+ /// struct MyType {
350
+ /// foo: i32,
351
+ /// }
352
+ ///
353
+ /// // Note: no #[methods] required
354
+ /// impl MyType {
355
+ /// pub fn get_foo(&self, _owner: TRef<Node>) -> i32 { self.foo }
356
+ /// pub fn set_foo(&mut self, _owner: TRef<Node>, val: i32) { self.foo = val; }
357
+ ///
358
+ /// fn my_register(builder: &ClassBuilder<MyType>) {
359
+ /// builder
360
+ /// .add_property("foo")
361
+ /// .with_default(5)
362
+ /// .with_hint((-10..=30).into())
363
+ /// .with_getter(MyType::get_foo)
364
+ /// .with_setter(MyType::set_foo)
365
+ /// .done();
366
+ /// }
367
+ /// }
327
368
/// ```
328
369
#[ inline]
329
370
pub fn add_property < ' a , T > ( & ' a self , name : & ' a str ) -> PropertyBuilder < ' a , C , T >
0 commit comments