Skip to content

Commit 50fca58

Browse files
added rpc mode decorators
1 parent dfbd376 commit 50fca58

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 makeRpcModeConfigDecorator(mode: godot.MultiplayerAPI.RPCMode) {
117+
return function <T extends godot.Node>(target: T, property: string, descriptor?: any) {
118+
const isMethod = typeof target[property] === 'function';
119+
const originalReady = target._ready;
120+
target._ready = function (this: godot.Node) {
121+
if (isMethod) this.rpc_config(property, mode);
122+
else this.rset_config(property, mode);
123+
if (originalReady) return originalReady.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 = makeRpcModeConfigDecorator(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 = makeRpcModeConfigDecorator(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 = makeRpcModeConfigDecorator(godot.MultiplayerAPI.RPC_MODE_PUPPET)
150+
/**
151+
* @deprecated Use `RPC_MODE_PUPPET` (@puppet) instead. Analogous to the `slave` keyword.
152+
*/
153+
export const slave = makeRpcModeConfigDecorator(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 = makeRpcModeConfigDecorator(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 = makeRpcModeConfigDecorator(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 = makeRpcModeConfigDecorator(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 = makeRpcModeConfigDecorator(godot.MultiplayerAPI.RPC_MODE_PUPPETSYNC)

0 commit comments

Comments
 (0)