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 CUSTOM_EVENT = 10000;
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 process is called by ObjectNet a Listener on an event.
This code illustrates how to listen to some network events, read data on these events, and take some action.
public void Start() {
NetworkManager.Events.RegisterEvent(CUSTOM_EVENT, this.OnCustomEventReceived);
}
private void OnCustomEventReceived(IDataStream reader) {
Vector3 receivedTargetPosition = reader.Read<Vector3>();
float remaningLife = reader.Read<float>();
int money = reader.Read<int>();
// Update player with received information
this.UpdatePlayer(receivedPosition, remaningLife, money);
}
On this piece of code, the CUSTOM_EVENT is registered to execute "OnCustomEventReceived" arrives.
The code of OnCustomEventReceived the first extract parameter in the same order that was sent. With this information, some method must be called to update an object, change status, create or destroy items, and another code to make your multiplayer game work.