Developer Documentation

OdinHandler.OnPeerJoined

public PeerJoinedProxy OnPeerJoined

Called on every Peer that joins the room(s) we're connected to

Info

Self is marked as Peer and this handler will trigger this invoke too

Value Type

TypeDescription
PeerJoinedProxy

Discussion

Whenever a “user (in ODIN language it’s a peer) joins a room, this event is triggered in all clients. You can listen to this event in your own code as shown in the example above. You can use this to fill an array with active players/users in your game.

The structure of the function looks like this (PeerJoinedProxy ):

public class PeerJoinedProxy : UnityEvent<object, PeerJoinedEventArgs>

In this example, taken from our Integration guide user data is stored in a CustomUserDataJsonFormat format (you can use any format you like). See our guides for more info on this topic.

Example

OnPeerJoined Example
public void OnPeerJoined (object sender, PeerJoinedEventArgs eventArgs)
{
    // Sender is typically a Room instance
    Room room = sender as Room;

    Debug.Log($"ODIN PEER JOINED. Room: {room.Config.Name}, PeerId: {eventInfo.PeerId}, UserData: {eventInfo.Peer.UserData.ToString()}");

    // Use CustomUserDataJsonFormat to parse UserData stored in JSON format
    CustomUserDataJsonFormat userData = CustomUserDataJsonFormat.FromUserData(eventInfo.Peer.UserData);
    if (userData.seed != null)
    {
        Debug.Log("Player has network Id: " + userData.seed);
        PlayerScript[] players = FindObjectsOfType<PlayerScript>();
        foreach (var player in players)
        {
            if (player.odinSeed == userData.seed)
            {
                Debug.Log("Found PlayerScript with seed " + userData.seed);
                if (player.isLocalPlayer)
                {
                    Debug.Log("Is local player, no need to do anything");
                }
                else
                {
                    // We have matched the OdinPeer with our local player instance
                }
            }
        }
    }
}