Skip to main content
Server Logic & Events

Not translated yet — showing English.

Cloud Functions & RPC

Keep the rules that matter on the server: the game asks, your server code decides. Server-side logic is a cloud function or a game server, deployed on its own. The game calls a cloud function with PlayServCode.CallAsync and gets a typed result; it calls a running game server with PlayServRpc.InvokeAsync.

using Playserv.Wrapper;
using UnityEngine;

var result = await PlayServCode.CallAsync<ClaimReward, RewardGranted>(
    "claim-match-reward", new ClaimReward { MatchId = matchId });

if (result.IsSuccess)
    Debug.Log($"+{result.Value.Coins} coins");
else
    Debug.LogWarning(result.Error.SourceCode);

Choose a route

PlayServCode.CallAsyncPlayServRpc.InvokeAsync
ReachesA deployed cloud functionA cloud function or a game server, by name
Goes overA request of its ownThe connection the SDK keeps open
Use it forRequest and answer: claim a reward, buy an item, validate a moveCalls into your running game server
VersionsCan call a tagged versionNo version choice

For plain reads and writes of your own data, you don't need either: use records.


Call a cloud function

Deploy the function

Write the function and deploy it with the CLI, see Deploying with the CLI. Its slug, such as claim-match-reward, is the name the game calls.

Let the game call it

The execute setting in the function's manifest must allow calls from the client. If its auth requires a player, the game must be signed in. See Functions → Settings.

Call it

CallAsync<TRequest, TResponse>(slug, body) sends body as JSON and reads the answer as TResponse. CallAsync<TResponse>(slug) calls without a body.

Option (PlayServFunctionCallOptions)What it does
VersionCalls a tagged version instead of the latest. An unknown tag fails with function_version_not_found; there is no fallback to the latest.
TimeoutSecondsHow long the game waits. The platform stops waiting for the function after 60 seconds, so a longer timeout changes nothing.
Query · HeadersQuery parameters and your own headers.
StrictResponseTypesFails when the answer doesn't match TResponse, instead of filling what it can.

CallAsync always sends a POST. For another method, or a raw body, use PlayServCode.InvokeAsync(new PlayServFunctionRequest { … }). InvokeBytesAsync and DownloadToFileAsync return binary answers; downloading to a file isn't available in WebGL.


Who is calling

The SDK sends the signed-in player's session with every call. The platform checks it and passes the player to your function, so the function knows who called. In a C# function it is Caller.PlayerId, with Caller.IsPlayer and Caller.IsAnonymous.

Never trust a player ID from the request body

Anyone can put any ID in the body. Take the player from the caller the platform passes, and check every value the game sends against your rules.


Call your game server (RPC)

using Playserv.RPC;
using Playserv.Wrapper;

PlayServRpcResult<RoundPreview> preview = await PlayServRpc.InvokeAsync<PreviewRequest, RoundPreview>(
    "arena", "round.preview", new PreviewRequest { Round = 3 });   // sent as "Round": names must match the handler's parameters, case included

InvokeAsync(service, method, request) sends the call over the game's connection. The service name is the name of a deployed function or game server: PlayServ turns it into kebab-case and drops a trailing Service, so "Arena" and "ArenaService" both reach arena. The method name is passed to it.

DetailValue
Timeout30 seconds by default; set another with PlayServRpcInvokeOptions.Timeout. A cloud function reached by RPC must answer within 20 seconds.
ResultIsSuccess, Value and Error. Timeouts and failures come back in the result, not as exceptions: Error.Code is Timeout, ServerError and so on, and UnifiedError.SourceCode names the platform's reason.
Without waitingPlayServRpc.Invoke(service, method, payload) sends the call and doesn't wait for the answer.
A game server with roomsOnly players taking part in one of its live rooms may call it.

A Unity game server answers RPC with handlers registered in a PlayServServerRpcRegistry, see Game Server Hosting.


Function call errors and limits

For CallAsync, errors come back in result.Error; Error.SourceCode says what happened, and Error.Retryable whether trying again can help. Only bad arguments, a missing client key, and cancellation are thrown.

SourceCodeWhen
not_foundNo function with that slug in this project and environment.
function_version_not_foundThe Version tag doesn't exist.
function_execute_forbiddenThe function doesn't accept calls from the game.
player_auth_requiredThe function needs a signed-in player.
function_unreachableThe function didn't answer within 60 seconds, or is down.
function_response_too_largeThe answer is over 16 MiB.
function_transport_failedA network failure. Retryable.

Any other status comes from your function itself, with its body in result.Response.

Make reward calls safe to repeat

A timeout doesn't mean the function didn't run: it may have finished after the game stopped waiting. For calls that grant something, pass an ID of the action, such as the match ID, and let the function ignore a second call with the same one.


Next steps

Last updated on 22 Oct 2026