Click or drag to resize

StrixBehaviourRpcToAll Method (String, Object)

Calls an RPC on all the clients in the match room including self.

Namespace:  SoftGear.Strix.Unity.Runtime
Assembly:  StrixUnityRuntime (in StrixUnityRuntime.dll) Version: 1.5.0
Syntax
C#
public void RpcToAll(
	string rpcName,
	params Object[] args
)

Parameters

rpcName
Type: SystemString
RPC method name.
args
Type: SystemObject
A list of method arguments. This should match the arguments in the method definition, both in number and respective types.
Remarks
The method has to be defined on the same class and marked with StrixRpcAttribute attribute.
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