forked from godot-rust/gdext
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#[rpc] annotation for functions in #[godot_api] inherent impl blocks.
The style is similar to GDScript's @rpc annotation, the macro can be used as follows: godot-rust#1 - Separate arguments: ```rust #[rpc(any_peer, reliable)] fn some_rpc(&mut self) { //.. } ``` Providing overlapping arguments generates a compile error. Any omitted arguments are set to their default values. godot-rust#2 - Provide an expression: ```rust const CONFIG: RpcArgs = RpcArgs { mode: RpcMode::Authority, ..RpcArgs::default() }; #[rpc(config = CONFIG_EXPR)] fn some_rpc(&mut self) { //.. } ``` Number godot-rust#2 is useful in case you want to reuse the configuration on multiple functions. Number godot-rust#2 is mutually exclusive with number godot-rust#1. --- The generated macro code works as follows: - Caches the configuration in a `ClassPlugin`. - On `__before_ready()`, searches for the configuration in the plugin, registering them with Node::rpc_config().
- Loading branch information
Showing
17 changed files
with
493 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) godot-rust; Bromeon and contributors. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use crate::builtin::{Dictionary, StringName}; | ||
use crate::classes::multiplayer_api::RpcMode; | ||
use crate::classes::multiplayer_peer::TransferMode; | ||
use crate::classes::Node; | ||
use crate::dict; | ||
use crate::meta::ToGodot; | ||
|
||
/// See [Godot documentation](https://docs.godotengine.org/en/stable/tutorials/networking/high_level_multiplayer.html#remote-procedure-calls) | ||
#[derive(Debug, Clone, Copy)] | ||
pub struct RpcArgs { | ||
pub mode: RpcMode, | ||
pub transfer_mode: TransferMode, | ||
pub call_local: bool, | ||
pub transfer_channel: u32, | ||
} | ||
|
||
impl Default for RpcArgs { | ||
fn default() -> Self { | ||
Self { | ||
mode: RpcMode::AUTHORITY, | ||
transfer_mode: TransferMode::UNRELIABLE, | ||
call_local: false, | ||
transfer_channel: 0, | ||
} | ||
} | ||
} | ||
|
||
impl RpcArgs { | ||
/// Register `method` as a remote procedure call on `node`. | ||
pub fn register(self, node: &mut Node, method: impl Into<StringName>) { | ||
node.rpc_config(method.into(), &self.into_dictionary().to_variant()); | ||
} | ||
|
||
/// Returns a [`Dictionary`] populated with the values required for a call to [`Node::rpc_config`]. | ||
pub fn into_dictionary(self) -> Dictionary { | ||
dict! { | ||
"mode": self.mode, | ||
"transfer_mode": self.transfer_mode, | ||
"call_local": self.call_local, | ||
"transfer_channel": self.transfer_channel, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.