Event Handling
Events in ODIN allow you to quickly customize the implementation of voice in your game or application.
Basic application flow
Have a look at this application flow of a very basic lobby application. Events that you need to implement are highlighted in red.
Step | Todo | Description |
---|---|---|
1 | Join Room | The user navigates to the multiplayer lobby. Here, all players currently playing the game are connected to the same ODIN room so that they can figure out what to play. The application uses JoinRoom function of theOdinHandler instance to join the room. Please note: The server automatically creates the room if it does not exist. There is no need for bookkeeping on your side. |
2 | RoomJoin | The OnRoomJoin event is triggered that allows you to handle logic before the user actually joins the room. Please note: All upcomingPeerJoined and MediaAdded events are for users that were already connected to the room. |
3 | PeerJoined | For each user connected to the same ODIN room you'll receive an OnPeerJoined event that allows you to handle logic once a peer joined the room. |
4 | MediaAdded | For each user connected to the same ODIN room which has a microphone stream enabled (some of them might only be spectators just listening) an OnMediaAdded event is triggered. This event needs to be handled by your application. In this callback, you basically use theAddPlaybackComponent member function of theOdinHandler singleton instance to create aPlaybackComponent that is attached to a GameObject in your scene, depending on your use case. |
5 | RoomJoined | The OnRoomJoined event is triggered that allows you to handle logic after the user joined the room. AllPeerJoined and MediaAdded events that come before RoomJoined event are for users that were already connected to the room. Events after RoomJoined event indicate changes to the room after the user connected. |
6 | MediaRemoved | Whenever a user disconnects from the room, or closes his microphone stream, an OnMediaRemoved event is triggered. You are responsible for cleaning up thePlaybackComponent components that you have created earlier in theMediaAdded callback function. |
7 | PeerLeft | Whenever a user disconnects from a room, this event is triggered. For example, you can show a push notification that a player has left the room. More info here: OnPeerLeft . Important notice:. Whenever a peer leaves a room, the media gets removed as well. So, aligned with this event, for each media of this peer, aMediaRemoved event will be triggered as well. |
8 | Leave Room | You can use the member function LeaveRoom of theOdinHandler singleton instance to leave a room. Important Notice: Due to the asynchronous nature of the leaving room operation, the current recommendation is to avoid invoking this function withinOnDestroy if the scene is being unloaded. Instead, the best practice is to call the LeaveRoom function and subsequently wait for theOnRoomLeft event to be triggered. Once this event has been triggered, it is then safe to perform further actions, such as callingLoadScene or Application.Quit . |
9 | RoomLeave | The event OnRoomLeave is triggered to notify you that the current user started to leave the room. You can use it to clean up your scene. You can either do that in this event or the next. |
10 | RoomLeft | The event OnRoomLeft is triggered to notify you that the current user has left the room. You need to listen to this event to do some cleanup work: You might have somePlaybackComponent components in your scene that you have created earlier. Some (or all of them) are linked to the room that the user just left. Use theDestroyPlaybackComponents member function of theOdinHandler singleton instance to remove allPlaybackComponent elements linked to the room left. |
Handling Notifications to users
Many applications notify users that other users have left or joined the room. If you show these notifications
whenever a PeerJoined
event is incoming, you'll show a notification for every user that is already connected to
the user. If you just want to notify users of changes after they have connected the room you have two options:
- The property
Self
of theRoom
is set in theRoomJoined
event. If this property is notnull
then you can be sure that the event indicates a change after the user connected. - You can set a local
Boolean
property in your class that isfalse
per default and is set totrue
in the RoomJoined event. In your PeerJoined event, you can check if this property is true or not. Only show notifications if this property is set totrue
.
Example Implementation
We have created a simple showcase demo in Unity. The idea is that you need to find other players in a foggy environment just by their voice. We leverage ODIN's built-in 3D positional audio that typically attaches the voice to player game objects so that their voice represents their location in 3D space - they are louder if close, and you might not hear them if they are far away. If they don't find each other, they can use a walkie-talkie-like functionality to talk to other players independently of their 3D location.
In the first step, we implement that basic walkie-talkie functionality by implementing these events:
OnMediaAdded OnMediaRemoved OnRoomLeft
Walkie-Talkie
This simple example works like this:
- All users are connected to the same ODIN room named "WalkieTalkie1."
- We provide an AudioMixerGroup with some audio effects added to the voice of other users, so they sound a bit distorted.
- In our example, the local player object is controlled by a
PlayerController
script that has awalkieTalkie
member variable that references a walkie-talkie mesh of the player character. - A singleton
GameManager
instance handles the creation of player objects and manages them. We use this class instance to get hold of our local player object to get a reference to the walkie-talkie game object that we need in the next step. - Whenever another user connects to a room, we handle the
OnMediaAdded
event and attach aPlaybackComponent
to this walkie-talkie mesh. Therefore, all audio sent by other players' voices is coming out of this walkie-talkie mesh.
The simplest way to do that is to create a new class in Unity (in our example we name it OdinPeerManager
) and to
implement those callback functions there. Then, either create an empty GameObject in your scene and attach the
OdinPeerManager
component to it or attach the OdinPeerManager
directly to the ODIN Manager prefab that you
already have in your scene and use the inspector of the Odin Manager prefab to
link the events of the ODIN SDK to your own implementation.
This is the player mesh we used in our ODIN example showcase. It's Elena from the Unity Asset Store, which looks stunning and is very easy to use. Highlighted is the walkie-talkie game object that is used to realistically attach all other players' voices to this object. Therefore, other players will hear walkie-talkie sound coming out of this player's walkie-talkie as you would in real life.
The final implementation of our OdinPeerManager
, implementing what we have defined above, looks like this:
What's left is that we need to join the room once the game starts. We do that in our PlayerController
script.
Switching channels
Walkie-talkies allow users to choose a channel so not everyone is talking on the same channel. We can add this functionality with a couple of lines of code. The only thing we need to do is to leave the current room representing a channel and join another room. That's it.
ODIN rooms are represented by their name. Nothing more. There is no bookkeeping required. Choose a name that makes sense for your application and join that room.
Joining the world room
All players running around in our scene join the same room, we simply call it "World." So, we adjust our current
Start
implementation in PlayerController
:
Adjusting OnMediaAdded
That's it. We now have joined the world room. What happens now is that all players' voices are attached to our walkie-talkie. Which is not what we want. We want to have the other players' walkie-talkies attached to our walkie-talkie, but the other players' "world-voice" we want to attach to the corresponding players in the scene so that their voice position is identical to their position in the scene.
Our current implementation of OnMediaAdded
Depending on the room where the media is added we need to handle things differently. If it's a walkie-talkie room,
we add the PlaybackComponent PlaybackComponent
If the room where media is added is a "WalkieTalkie" room, we use the same implementation used before. However, if it's
the world room, we need to find the corresponding player game object in the scene and attach
the PlaybackComponent spatialBlend
1.0
to activate 3D positional audio,
that is, Unity will handle 3D audio for us automatically.
This guide should show you how to do the basic and required event handling. We don't go into much detail on how to merge your multiplayer framework with ODIN. Depending on the multiplayer framework there are different solutions on how to do that. We show a typical solution in our Mirror Networking guide and have an open-source example available for Photon.