Network Event

NetworkEvent Documentation

The NetworkEvent component allows you to send and receive custom network events between clients in a Metaverse scene. This can be useful for triggering events on other clients, such as playing animations or sounds.

Namespace

MetaverseCloudEngine.Unity.Networking.Components

Public Methods

  • void SendEvent(): Sends this event to the receivers specified in the receivers field.

Public Properties

  • string EventID: The unique identifier of this event.

  • NetworkMessageReceivers Receivers: The receivers of this event. This can be set to All, Others, or Host.

  • bool Buffered: Whether this event should be buffered. Buffered events are sent to clients that join the scene after the event was sent.

Inspector Fields

  • string eventID: The unique identifier of this event.

  • NetworkMessageReceivers receivers: The receivers of this event.

  • bool buffered: Whether this event should be buffered.

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

Behavior

  • Execution Order: This class inherits from MetaSpaceBehaviour, which uses the default Unity execution order.

Usage

  1. To use NetworkEvent, start by adding the component to a GameObject in your scene.

  2. In the inspector, set the eventID property to a unique identifier for this event.

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

  4. Optionally, set the buffered property to true if you want the event to be buffered.

  5. Add a Unity Event to the onNetworkEvent event to specify what should happen when the event is received.

  6. You can then use the SendEvent() method to send the event to the specified receivers.

How NetworkEvent Works

The NetworkEvent component works by sending a network event message to the server or other clients, depending on the value of the receivers property. The message contains the eventID of the event.

When a client receives a network event message, it checks to see if it has a NetworkEvent component with the same eventID. If it does, it invokes the onNetworkEvent event on that component.

This allows you to easily communicate events between clients.

Example:

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

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

  2. In the inspector, set the eventID property to "PlayerJumped".

  3. Set the receivers property to Others.

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

  5. In your player controller script, call the SendEvent() method on the NetworkEvent component whenever the player jumps.

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

Last updated