0
0
Unityframework~10 mins

Remote procedure calls in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Remote procedure calls
Client calls function
Serialize data
Send data over network
Server receives data
Deserialize data
Execute function on server
Serialize response
Send response back
Client receives response
Deserialize response
Use result in client
This flow shows how a client calls a function on a server by sending data, the server runs the function, and sends back the result.
Execution Sample
Unity
using Unity.Netcode;

public class Example : NetworkBehaviour {
    [ServerRpc]
    public void AddNumbersServerRpc(int a, int b) {
        int sum = a + b;
        Debug.Log($"Sum is {sum}");
    }
}
This code shows a server RPC in Unity that adds two numbers sent from a client and logs the sum on the server.
Execution Table
StepActionData SentServer ActionResult/Output
1Client calls AddNumbersServerRpc(3, 4)a=3, b=4None yetNone yet
2Serialize dataSerialized (3,4)None yetNone yet
3Send data over networkSerialized (3,4)None yetNone yet
4Server receives dataSerialized (3,4)Deserialize dataa=3, b=4
5Execute AddNumbersServerRpc on servera=3, b=4Calculate sum = 7Log: Sum is 7
6Serialize response (if any)None (void)Prepare responseNone
7Send response back to clientNoneNoneNone
8Client receives responseNoneNoneNone
9Client uses result (none here)NoneNoneNone
10End of RPC callNoneNoneSum logged on server
💡 RPC call ends after server executes function and client receives any response.
Variable Tracker
VariableStartAfter Step 4After Step 5Final
aundefined333
bundefined444
sumundefinedundefined77
Key Moments - 3 Insights
Why does the client not execute the function directly?
Because the function is marked as ServerRpc, it runs only on the server. The client sends data to the server, which executes the function (see execution_table step 5).
What happens to the data when sent over the network?
The data is serialized into a format that can be sent over the network (step 2 and 3), then deserialized on the server (step 4) to restore the original values.
Why is there no result sent back to the client in this example?
The ServerRpc function returns void, so no data is sent back. The client only triggers the server to run the function (see steps 6-9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'sum' after step 5?
Aundefined
B3
C7
D4
💡 Hint
Check the 'sum' variable in variable_tracker after step 5.
At which step does the server deserialize the data sent from the client?
AStep 4
BStep 2
CStep 6
DStep 8
💡 Hint
Look at the 'Server Action' column in the execution_table.
If the ServerRpc function returned a value, what would change in the execution table?
AThe client would execute the function instead of the server.
BThere would be serialized response data sent back to the client.
CThe data would not be serialized before sending.
DThe server would not log the sum.
💡 Hint
Refer to steps 6-9 about response handling in the execution_table.
Concept Snapshot
Remote Procedure Calls (RPC) in Unity:
- Client calls a function marked with [ServerRpc]
- Data is serialized and sent to the server
- Server deserializes and runs the function
- Server can send back a response
- Client receives and uses the response
- RPCs enable communication between client and server code
Full Transcript
Remote procedure calls in Unity let a client ask the server to run a function. The client calls a function marked with [ServerRpc]. The data is packed up (serialized) and sent over the network. The server unpacks (deserializes) the data and runs the function. The server can send back a result, which the client receives and uses. This process helps client and server talk and work together in a game.