Listening Events
This section covers how to Listen to events over the network.
First, you need to create or use a unique identifier for this event, this identifier shall be a positive integer number.
const int LOCAL_CUSTOM_EVENT = 20000;
Note: Is highly recommenced to group events to avoid two events using the same code.
You can listen to events sent by other network peers and execute some action based on this event, this event will arrive specifically on the same object where the event will be raised on the server or another remote player.
This code illustrates how to listen to some network event from a NetworkObject, read data on this event, and take some action.
public void Start() {
this.RegisterEvent(LOCAL_CUSTOM_EVENT, this.OnCustomEventReceivedOnObject);
}
private void OnCustomEventReceivedOnObject(IDataStream reader) {
Vector3 receivedPosition = reader.Read<Vector3>();
float receivedStatus = reader.Read<float>();
int receivedErrorCode = reader.Read<ushort>();
// Update player with received information
this.RaiseObjectError(receivedPosition, receivedStatus, receivedErrorCode);
}
On this piece of code, the LOCAL_CUSTOM_EVENT is registered to execute "OnCustomEventReceivedOnObject" arrives.
The code of OnCustomEventReceivedOnObject first extracts parameters in the same order they were sent. With this information, some method must be called on this object to update an object, change the status, create or destroy items, and another code to make your multiplayer game work.