Function rpcCall

  • Makes the function an RPC (remote procedure call) that can be invoked from clients to the server or from the server to clients. The decorated method must be applied on a class that can provide a NetObjectId. Currently, the support types are subclasses of Component.

    For TargetClient and TargetMulticast scopes, the decorated method must accept RpcCallParams as its last argument, which allows configuring timeout, reliability, and specifying target clients.

    Parameters

    • rpcScope: Server | OwnerClient

      The scope of the RPC, which determines the direction and target of the call (e.g., Server, OwnerClient, TargetClient, Multicast).

    • Optionalreliable: boolean

      Whether the RPC call should be reliable (guaranteed delivery). Defaults to true.

    Returns (<TArgs, TReturn>(target: Component, key: string, descriptor: TypedPropertyDescriptor<RpcMethod<TArgs, TReturn>>) => void)

    rpcCall(NetRpcScope.Server)
    public async requestSpawn(prefabId: string): Promise<number>
    {
    // This code runs on the server when invoked from a client.
    // Returns the spawned entity ID back to the calling client.
    }
    rpcCall(NetRpcScope.OwnerClient)
    public async notifyOwner(message: string): Promise<void>
    {
    // This code runs on the owning client of the net object.
    // Called from the server to notify the owner.
    }
    rpcCall(NetRpcScope.TargetClient)
    public async sendPrivateMessage(message: string, params: RpcCallParams): Promise<void>
    {
    // This code runs on a specific client specified in params.
    // Call with: this.sendPrivateMessage("Hello", { targetConnectionIds: [clientId] });
    }
    rpcCall(NetRpcScope.TargetMulticast)
    public async notifyTeam(alert: string, params: RpcCallParams): RpcMulticastPromise<boolean>
    {
    // This code runs on multiple specific clients specified in params.
    // The implementation returns the raw value (boolean).
    return true;
    }

    // Calling - use rpcUnwrap() or rpcUnwrapReturn() to get the wrapped results:
    const results = rpcUnwrap(await this.notifyTeam("Alert!", { targetConnectionIds: [id1, id2, id3] }));
    for (const result of results)
    {
    if (result.isSuccess)
    console.log(`Client ${result.connectionId} returned: ${result.returnValue}`);
    }
    rpcCall(NetRpcScope.Multicast)
    public async broadcastEvent(eventData: string): RpcMulticastPromise<void>
    {
    // This code runs on all connected clients.
    // The implementation returns void.
    console.log(`Received event: ${eventData}`);
    return true;
    }

    // Calling - use rpcUnwrapReturn() to get the wrapped results:
    const results:boolean[] = rpcUnwrapReturn(await this.broadcastEvent("GameStarted"));
    console.log(`Broadcast reached ${results.filter(r => r == true).length} clients`);
  • Parameters

    Returns (<TArgs, TReturn>(target: Component, key: string, descriptor: TypedPropertyDescriptor<RpcMethodMulticast<TArgs, TReturn>>) => void)

  • Parameters

    Returns (<TArgs, TReturn>(target: Component, key: string, descriptor: TypedPropertyDescriptor<RpcMethodParams<TArgs, TReturn>>) => void)

  • Parameters

    Returns (<TArgs, TReturn>(target: Component, key: string, descriptor: TypedPropertyDescriptor<RpcMethodTargetMulticast<TArgs, TReturn>>) => void)