Skip to content

Commit 5b1d7f3

Browse files
committed
Added documentation to the NativeClass
1 parent 633c8c7 commit 5b1d7f3

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

gdnative-derive/src/lib.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,43 @@ mod profiled;
1616
mod varargs;
1717
mod variant;
1818

19+
/// Collects method signatures of all functions in a `NativeClass` that have the `#[export]` attribute and registers them with Godot.
20+
///
21+
/// For example, in the following class
22+
/// ```ignore
23+
/// #[derive(NativeClass)]
24+
/// #[inherit(Reference)]
25+
/// #[no_constructor]
26+
/// struct Foo {}
27+
///
28+
/// #[methods]
29+
/// impl Foo {
30+
/// #[export]
31+
/// fn foo(&self, _owner: &Reference, bar: i64) -> i64 {
32+
/// bar
33+
/// }
34+
/// }
35+
///
36+
/// ```
37+
/// Will expand to
38+
/// ```ignore
39+
/// struct Foo{}
40+
/// impl gdnative::nativescript::NativeClassMethods for Foo {
41+
/// fn register(builder: &ClassBuilder<Self>) {
42+
/// use gdnative::nativescript::init::*;
43+
/// builder.build_method("foo", gdnative::godot_wrap_method!(Foo::foo))
44+
/// .with_rpc_mode(Rpc::Disabled)
45+
/// .done_stateless();
46+
/// }
47+
///
48+
/// fn foo(&self, _owner: &Reference, bar: i64) -> i64 {
49+
/// bar
50+
/// }
51+
/// }
52+
/// ```
53+
/// **Important**: Only one `impl` block per struct may be attributed with `#[methods]`.
54+
///
55+
/// For more context, please refer to [gdnative::NativeClass](NativeClass).
1956
#[proc_macro_attribute]
2057
pub fn methods(meta: TokenStream, input: TokenStream) -> TokenStream {
2158
if syn::parse::<syn::parse::Nothing>(meta.clone()).is_err() {
@@ -164,6 +201,49 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
164201
/// - `no_editor`
165202
///
166203
/// Hides the property from the editor. Does not prevent it from being sent over network or saved in storage.
204+
///
205+
/// ### `#[methods]`
206+
/// Adds the necessary information to a an `impl` block to register the properties and methods with Godot.
207+
///
208+
/// **Important**: This needs to be added to one and only one `impl` block for a given `NativeClass`.
209+
///
210+
/// For additional details about how `#[methods]` expands, please refer to [gdnative::methods](methods)
211+
///
212+
/// ### `#[export]`
213+
/// Registers the attributed function signature to be used by Godot.
214+
/// A valid function signature must have:
215+
/// - `&self` or `&mut self` as its first parameter
216+
/// - `&T` or `TRef<T>` where T refers to the type declared in `#[inherit(T)]` attribute as it's second parameter; this is typically called the `owner`.
217+
/// - Optionally, any number of additional parameters, which must have the type `Variant` or must implement the `FromVariant` trait. `FromVariant` is implemented for most common types.
218+
/// - Return values must implement the `OwnedToVariant` trait (automatically implemented by `ToVariant`)
219+
/// or be a `Variant` type.
220+
///
221+
/// ```ignore
222+
/// #[export]
223+
/// fn foo(&self, owner: &Reference)
224+
/// ```
225+
/// **Note**: Marking a function with `#[export]` does not have any effect unless inside an `impl` block that has the `#[methods]` attribute.
226+
///
227+
/// Possible arguments for this attribute are
228+
///
229+
/// - `name` = "overridden_function_name"
230+
///
231+
/// Overrides the function name as the method name to be registered in Godot.
232+
///
233+
/// - `rpc` = "selected_rpc"
234+
/// - "selected_rpc" must be one of the following values, which refer to the associated [Multiplayer API RPC Mode](https://docs.godotengine.org/en/stable/classes/class_multiplayerapi.html?highlight=RPC#enumerations)
235+
/// - "disabled" - `RPCMode::RPC_MODE_DISABLED`
236+
/// - "remote" - `RPCMode::RPC_MODE_REMOTE`
237+
/// - "remote_sync" - `RPCMode::RPC_MODE_REMOTE_SYMC`
238+
/// - "master" - `RPCMode::RPC_MODE_MASTER`
239+
/// - "puppet" - `RPCMode::RPC_MODE_PUPPET`
240+
/// - "master_sync" - `RPCMode::RPC_MODE_MASTERSYNC`
241+
/// - "puppet_sync" - `RPCMode::RPC_MODE_PUPPETSYNC`
242+
///
243+
/// This enables you to set the [Multiplayer API RPC Mode](https://docs.godotengine.org/en/stable/classes/class_multiplayerapi.html?highlight=RPC#enumerations) for the function.
244+
/// Refer to [Godot's Remote Procedure documentation](https://docs.godotengine.org/en/stable/tutorials/networking/high_level_multiplayer.html#rpc) for more details.
245+
///
246+
167247
#[proc_macro_derive(
168248
NativeClass,
169249
attributes(

0 commit comments

Comments
 (0)