Skip to content

Commit ea56ec1

Browse files
authored
Merge pull request #53 from MichaelBelousov/multiplayer-decorators
added rpc mode decorators
2 parents b81b896 + 4c4ed75 commit ea56ec1

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

misc/decorators.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,61 @@ export function enumeration<T extends godot.Object>(enumeration: string[], defau
112112
return descriptor;
113113
}
114114
}
115+
116+
function make_rpc_mode_config_decorator(mode: godot.MultiplayerAPI.RPCMode) {
117+
return function <T extends godot.Node>(target: T, property: string, descriptor?: any) {
118+
const is_method = typeof target[property] === 'function';
119+
const original_ready = target._ready;
120+
target._ready = function (this: godot.Node) {
121+
if (is_method) this.rpc_config(property, mode);
122+
else this.rset_config(property, mode);
123+
if (original_ready) return original_ready.call(this);
124+
};
125+
return descriptor;
126+
}
127+
}
128+
129+
/**
130+
* Used with `Node.rpc_config` or `Node.rset_config` to set a method to be called or
131+
* a property to be changed only on the remote end, not locally.
132+
* Analogous to the `remote` keyword. Calls and property changes are accepted from all
133+
* remote peers, no matter if they are node's master or puppets.
134+
*/
135+
export const remote = make_rpc_mode_config_decorator(godot.MultiplayerAPI.RPC_MODE_REMOTE)
136+
/**
137+
* Used with `Node.rpc_config` or `Node.rset_config` to set a method to be called or
138+
* a property to be changed only on the network master for this node.
139+
* Analogous to the `master` keyword. Only accepts calls or property changes from the
140+
* node's network puppets, see `Node.set_network_master`.
141+
*/
142+
export const master = make_rpc_mode_config_decorator(godot.MultiplayerAPI.RPC_MODE_MASTER)
143+
/**
144+
* Used with `Node.rpc_config` or `Node.rset_config` to set a method to be called or
145+
* a property to be changed only on puppets for this node.
146+
* Analogous to the `puppet` keyword. Only accepts calls or property changes from the
147+
* node's network master, see `Node.set_network_master`.
148+
*/
149+
export const puppet = make_rpc_mode_config_decorator(godot.MultiplayerAPI.RPC_MODE_PUPPET)
150+
/**
151+
* @deprecated Use `RPC_MODE_PUPPET` (@puppet) instead. Analogous to the `slave` keyword.
152+
*/
153+
export const slave = make_rpc_mode_config_decorator(godot.MultiplayerAPI.RPC_MODE_SLAVE)
154+
/**
155+
* Behave like `RPC_MODE_REMOTE` (@remote) but also make the call or property change locally.
156+
* Analogous to the `remotesync` keyword.
157+
*/
158+
export const remote_sync = make_rpc_mode_config_decorator(godot.MultiplayerAPI.RPC_MODE_REMOTESYNC)
159+
/**
160+
* @deprecated Use `RPC_MODE_REMOTESYNC` (@remote_sync) instead. Analogous to the `sync` keyword
161+
*/
162+
export const sync = make_rpc_mode_config_decorator(godot.MultiplayerAPI.RPC_MODE_REMOTESYNC)
163+
/**
164+
* Behave like `RPC_MODE_MASTER` (@master) but also make the call or property change locally.
165+
* Analogous to the `mastersync` keyword.
166+
*/
167+
export const master_sync = make_rpc_mode_config_decorator(godot.MultiplayerAPI.RPC_MODE_MASTERSYNC)
168+
/**
169+
* Behave like `RPC_MODE_PUPPET` (@puppet) but also make the call or property change locally.
170+
* Analogous to the `puppetsync` keyword.
171+
*/
172+
export const puppet_sync = make_rpc_mode_config_decorator(godot.MultiplayerAPI.RPC_MODE_PUPPETSYNC)

0 commit comments

Comments
 (0)