@@ -518,6 +518,7 @@ pub fn derive_godot_class(input: TokenStream) -> TokenStream {
518
518
/// - [User-defined functions](#user-defined-functions)
519
519
/// - [Associated functions and methods](#associated-functions-and-methods)
520
520
/// - [Virtual methods](#virtual-methods)
521
+ /// - [RPC attributes](#rpc-attributes)
521
522
/// - [Constants and signals](#signals)
522
523
///
523
524
/// # Constructors
@@ -676,6 +677,75 @@ pub fn derive_godot_class(input: TokenStream) -> TokenStream {
676
677
///
677
678
/// Make sure you understand the limitations in the [tutorial](https://godot-rust.github.io/book/register/virtual-functions.html).
678
679
///
680
+ /// ## RPC attributes
681
+ ///
682
+ /// You can use the `#[rpc]` attribute to let your functions act as remote procedure calls (RPCs) in Godot. This is the Rust equivalent of
683
+ /// GDScript's [`@rpc` annotation](https://docs.godotengine.org/en/stable/tutorials/networking/high_level_multiplayer.html#remote-procedure-calls).
684
+ /// `#[rpc]` is only supported for classes inheriting `Node`, and they need to declare a `Base<T>` field.
685
+ ///
686
+ /// The syntax follows GDScript'a `@rpc`. You can optionally specify up to four keys; omitted ones use their default value.
687
+ /// Here's an overview:
688
+ ///
689
+ /// | Setting | Type | Possible values (first is default) |
690
+ /// |---------------|------------------|----------------------------------------------------|
691
+ /// | RPC mode | [`RpcMode`] | **`authority`**, `any_peer` |
692
+ /// | Sync | `bool` | **`call_remote`**, `call_local` |
693
+ /// | Transfer mode | [`TransferMode`] | **`unreliable`**, `unreliable_ordered`, `reliable` |
694
+ /// | Channel | `u32` | any |
695
+ ///
696
+ /// You can also use `#[rpc(config = value)]`, with `value` being an expression of type [`RpcConfig`] in scope, for example a `const` or the
697
+ /// call to a function. This can be useful to reuse configurations across multiple RPCs.
698
+ ///
699
+ /// `#[rpc]` implies `#[func]`. You can use both attributes together, if you need to configure other `#[func]`-specific keys.
700
+ ///
701
+ /// For example, the following method declarations are all equivalent:
702
+ /// ```
703
+ /// use godot::classes::multiplayer_api::RpcMode;
704
+ /// use godot::classes::multiplayer_peer::TransferMode;
705
+ /// use godot::prelude::*;
706
+ /// use godot::register::RpcConfig;
707
+ ///
708
+ /// # #[derive(GodotClass)]
709
+ /// # #[class(no_init, base=Node)]
710
+ /// # struct MyStruct {
711
+ /// # base: Base<Node>,
712
+ /// # }
713
+ /// #[godot_api]
714
+ /// impl MyStruct {
715
+ /// #[rpc(unreliable_ordered, channel = 2)]
716
+ /// fn with_defaults(&mut self) {}
717
+ ///
718
+ /// #[rpc(authority, unreliable_ordered, call_remote, channel = 2)]
719
+ /// fn explicit(&mut self) {}
720
+ ///
721
+ /// #[rpc(config = MY_RPC_CONFIG)]
722
+ /// fn external_config_const(&mut self) {}
723
+ ///
724
+ /// #[rpc(config = my_rpc_provider())]
725
+ /// fn external_config_fn(&mut self) {}
726
+ /// }
727
+ ///
728
+ /// const MY_RPC_CONFIG: RpcConfig = RpcConfig {
729
+ /// rpc_mode: RpcMode::AUTHORITY,
730
+ /// transfer_mode: TransferMode::UNRELIABLE_ORDERED,
731
+ /// call_local: false,
732
+ /// channel: 2,
733
+ /// };
734
+ ///
735
+ /// fn my_rpc_provider() -> RpcConfig {
736
+ /// RpcConfig {
737
+ /// transfer_mode: TransferMode::UNRELIABLE_ORDERED,
738
+ /// channel: 2,
739
+ /// ..Default::default() // only possible in fn, not in const.
740
+ /// }
741
+ /// }
742
+ /// ```
743
+ ///
744
+ // Note: for some reason, the intra-doc links don't work here, despite dev-dependency on godot.
745
+ /// [`RpcMode`]: ../classes/multiplayer_api/struct.RpcMode.html
746
+ /// [`TransferMode`]: ../classes/multiplayer_peer/struct.TransferMode.html
747
+ /// [`RpcConfig`]: ../register/struct.RpcConfig.html
748
+ ///
679
749
/// # Constants and signals
680
750
///
681
751
/// Please refer to [the book](https://godot-rust.github.io/book/register/constants.html).
0 commit comments