Network Object RPC

NetworkObjectRpc Documentation

The NetworkObjectRpc component can be added to a NetworkObject to allow it to receive RPCs (Remote Procedure Calls) from the server and other clients. RPCs are used to communicate events between networked objects and players.

Namespace

MetaverseCloudEngine.Unity.Networking.Components

Public Methods

  • void SendRpc(): Sends this RPC to the receivers specified in the receivers field.

Public Properties

  • RpcReceiver receivers: The receivers of this RPC. This can be set to All, Others, Host, StateAuthority, or InputAuthority.

  • UnityEvent onReceive: The event that will be invoked when this RPC is received.

Inspector Fields

  • string rpcID: The unique identifier of this RPC. This ID is used to identify the RPC when it is received by other clients.

  • RpcReceiver receivers: The receivers of this RPC.

  • UnityEvent onReceive: The event that will be invoked when this RPC is received.

Behavior

  • Execution Order: This class inherits from NetworkObjectBehaviour, which uses the ExecutionOrder.Finalize execution order.

Usage

  1. To use NetworkObjectRpc, start by adding the component to a GameObject in your scene. This GameObject should also have a NetworkObject component.

  2. The rpcID is set internally by the component.

  3. Set the receivers property to the desired receivers for this RPC.

  4. Add a Unity Event to the onReceive event to specify what should happen when the RPC is received.

  5. You can then use the SendRpc() method to send the RPC to the specified receivers.

How NetworkObjectRpc Works

The NetworkObjectRpc component works by sending an RPC message to the server or other clients, depending on the value of the receivers property. The message contains the rpcID of the RPC.

When a client receives an RPC message, it checks to see if it has a NetworkObjectRpc component. If it does, it invokes the onReceive event on that component.

This allows you to easily communicate events between networked objects and players.

Example:

You can use the NetworkObjectRpc component to tell all other clients that a player has jumped. To do this:

  1. Add the NetworkObjectRpc component to the player's GameObject.

  2. Set the receivers property to Others.

  3. Add a Unity Event to the onReceive event that plays an animation on the player's avatar.

  4. In your player controller script, call the SendRpc() method on the NetworkObjectRpc component whenever the player jumps.

This will play the animation on all other clients whenever the player jumps.

Last updated