|
| 1 | +--- |
| 2 | +sidebar_position: 999 |
| 3 | +title: Remote Procedure Calls (RPC) |
| 4 | +--- |
| 5 | + |
| 6 | +# Remote Procedure Calls |
| 7 | + |
| 8 | +You can use the `@rpc` decorators to mark behavior functions as remote procedures. RPC decorated functions **must** have |
| 9 | +all arguments (and in some cases return types) serializable to `JsonValue`. |
| 10 | + |
| 11 | +## Server |
| 12 | + |
| 13 | +Annotate a function with `@rpc.server` to mark it as server-only. Regardless of where this function is called, it will |
| 14 | +be ran on the server. Server RPC functions **must** be async (or return a promise) as they network the result back. This |
| 15 | +allows you to query for data that only the server is able to access (ie: [KV data](./kv.mdx)). |
| 16 | + |
| 17 | +### Examples |
| 18 | + |
| 19 | +Run a `hello` function on the server that logs out the client that called it. |
| 20 | + |
| 21 | +```ts |
| 22 | +import { Behavior, rpc } from "@dreamlab/engine"; |
| 23 | + |
| 24 | +export default class RpcBehavior extends Behavior { |
| 25 | + @rpc.server() |
| 26 | + async hello(from: string): Promise<void> { |
| 27 | + // check server logs to see these messages |
| 28 | + console.log("hello from: " + target); |
| 29 | + } |
| 30 | + |
| 31 | + onInitialize(): void { |
| 32 | + // get our own network id and run rpc function |
| 33 | + const self = this.game.network.self; |
| 34 | + this.hello(self); |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +Get some server KV data and return it to the client. |
| 40 | + |
| 41 | +```ts |
| 42 | +import { Behavior, JsonValue, rpc } from "@dreamlab/engine"; |
| 43 | + |
| 44 | +export default class RpcBehavior extends Behavior { |
| 45 | + @rpc.server() |
| 46 | + async getData(): Promise<JsonValue> { |
| 47 | + // required type guard |
| 48 | + // decorators are unable to do type narrowing so it must be done manually |
| 49 | + if (!this.game.isServer()) throw new Error("rpc not on server"); |
| 50 | + |
| 51 | + // get data from server kv |
| 52 | + const kv = this.game.kv.server; |
| 53 | + const data: JsonValue = await kv.get("my-data"); |
| 54 | + |
| 55 | + return data; |
| 56 | + } |
| 57 | + |
| 58 | + onInitialize(): void { |
| 59 | + // only run on client |
| 60 | + if (!this.game.isClient()) return; |
| 61 | + |
| 62 | + // get data from server |
| 63 | + this.getData().then((data) => { |
| 64 | + console.log(data); |
| 65 | + }); |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +## Broadcast |
| 71 | + |
| 72 | +Annotate a function with `@rpc.broadcast` to mark it as broadcast. This will make all connected clients run the function |
| 73 | +at the same time whenever it is called. Broadcast functions **must** return `void` or `Promise<void>` as they cannot |
| 74 | +have return values. This allows you to synchronize actions between clients (ie: spawning effects locally, playing |
| 75 | +sounds, etc). |
| 76 | + |
| 77 | +:::info |
| 78 | + |
| 79 | +Broadcast functions can also define a target of either `all` or `only-clients` (default: `all`) which allows you to |
| 80 | +optionally omit the server from running the function. |
| 81 | + |
| 82 | +::: |
| 83 | + |
| 84 | +### Examples |
| 85 | + |
| 86 | +Spawn effect prefab on all clients. |
| 87 | + |
| 88 | +```ts |
| 89 | +import { Behavior, Entity, EntityRef, rpc, value } from "@dreamlab/engine"; |
| 90 | + |
| 91 | +export default class RpcBehavior extends Behavior { |
| 92 | + @value({ type: EntityRef }) |
| 93 | + effect: Entity; |
| 94 | + |
| 95 | + @rpc.broadcast({ target: "only-clients" }) |
| 96 | + spawnEffect(): void { |
| 97 | + // required type guard |
| 98 | + // decorators are unable to do type narrowing so it must be done manually |
| 99 | + if (!this.game.isClient()) return; |
| 100 | + |
| 101 | + // spawn effect prefab into local root |
| 102 | + // this assumes the prefab is responsible for its own cleanup |
| 103 | + this.effect.cloneInto(this.game.local); |
| 104 | + } |
| 105 | + |
| 106 | + onInitialize(): void { |
| 107 | + // only run on client |
| 108 | + if (!this.game.isClient()) return; |
| 109 | + |
| 110 | + // spawn effect on all clients |
| 111 | + this.spawnEffect(); |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
0 commit comments