Data class containing the information about the received RPC call.
Inheritance Hierarchy
Namespace:
SoftGear.Strix.Unity.Runtime
Assembly:
StrixUnityRuntime (in StrixUnityRuntime.dll) Version: 1.5.0
Syntax public class StrixRpcContext
The StrixRpcContext type exposes the following members.
Constructors Properties
| Name | Description |
---|
| sender |
Information about the match room member who sent the RPC.
|
| senderUid |
UID of the client who sent the RPC.
|
TopRemarks
If you add this class as the last argument of a method marked with
StrixRpcAttribute attribute,
each time the RPC is called the information about the sender will be automatically supplied.
You should not pass this argument manually when calling the RPC.
Examples
In this example we use RPC to update character's health instead of a
StrixSyncField in order to add a visual effect when the hit was strong enough.
We also use
StrixRpcContext to get information about the client who sent the RPC.
OnMouseDown method is called by Unity when you click this object (the object also needs a collider).
When this happens we invoke an RPC using
RpcToAll method and pass the name of the method ("Hit") along with the integer damage argument.
Note that the actual Hit method has two arguments instead of one. The
StrixRpcContext argument is a special optional argument the data for which is supplied by Strix.
Also note that we used
1 as a
ProcedureCode in
StrixRpc attribute.
This is done to minimize the RPC network packet size since smaller values are better compressed. Although we have to make sure that we use each code only once.
All the clients that receive the RPC check the damage amount independently and spawn a blood visual effect if the damage was greater than or equal to 5.
This way the blood effect is fully local for each client and doesn't have to be replicated, and as a result the network traffic will also be reduced.
using SoftGear.Strix.Unity.Runtime;
using UnityEngine;
public class RpcExample : StrixBehaviour
{
public GameObject BloodPrefab;
public int Health = 100;
private void OnMouseDown()
{
RpcToAll("Hit", Random.Range(3, 8));
}
[StrixRpc(ProcedureCode = 1)]
public void Hit(int damage, StrixRpcContext strixRpcContext)
{
Health = Mathf.Max(0, Health - damage);
if (damage >= 5)
Instantiate(BloodPrefab, transform.position, Quaternion.identity);
Debug.Log(name + " received " + damage + " damage from " + strixRpcContext.sender.GetName());
}
}
See Also