1- //! Types and functionalities to declare and initialize gdnative classes.
1+ //! Low-level API to register and export GDNative classes, methods and properties .
22//!
3- //! ## API endpoints
3+ //! ## Init and exit hooks
44//!
55//! Three endpoints are automatically invoked by the engine during startup and shutdown:
66//!
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`],
1010//!
1111//! 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`].
1313//!
1414//! ## Registering script classes
1515//!
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:
1819//!
1920//! ```ignore
20- //! // - snip -
21+ //! use gdnative::prelude::*;
2122//!
22- //! fn init(handle: gdnative::init:: InitHandle) {
23+ //! fn init(handle: InitHandle) {
2324//! handle.add_class::<HelloWorld>();
2425//! }
2526//!
2627//! godot_init!(init);
27- //!
28- //! // - snip -
2928//! ```
3029//!
3130//! For full examples, see [`examples`](https://github.com/godot-rust/godot-rust/tree/master/examples)
@@ -291,15 +290,39 @@ impl<C: NativeClass> ClassBuilder<C> {
291290 /// # Examples
292291 ///
293292 /// 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 {}
294301 ///
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 }
297305 ///
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+ /// }
302324 /// ```
325+ ///
303326 #[ inline]
304327 pub fn build_method < ' a , F : Method < C > > (
305328 & ' a self ,
@@ -316,14 +339,32 @@ impl<C: NativeClass> ClassBuilder<C> {
316339 ///
317340 /// Basic usage:
318341 ///
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+ /// }
327368 /// ```
328369 #[ inline]
329370 pub fn add_property < ' a , T > ( & ' a self , name : & ' a str ) -> PropertyBuilder < ' a , C , T >
0 commit comments