This sample demonstrates how to implement the match lobbies feature. A match lobby is an entity that allows a set of players to be matched and to play together.
The match lobby data is stored in a Cosmos DB instance, allowing players to list and join any available lobby. It includes the identifier of the Party network to which the players will join to, and across which players will share the game data.
Besides, this sample implements the lobby locking feature. This allows the host to lock the match lobby, generating an invitation code that the guest will need to use when they try to join. To do this, the match lobby stored in Cosmos DB includes the Locked
attribute. The invitation code - named as invitation id
by PlayFab - is included inside the network ID during the network creation by the Party SDK.
For more information about this feature see the join document.
- Read and complete the PlayFab configuration.
- Read and complete the Azure Function configuration.
- Read and complete the Cosmos DB configuration.
- Read the Search Match Lobby implementation guide.
This is the current Architecture we're using for implementing the match lobby feature:
The implementation of the match lobby creation feature has the following steps:
The Unity Game is the first layer involved. The first step is creating the Party network, making the game execute the CreateAndJoinToNetwork
method from the PartyNetworkHandler
, which waits for the network identifier returned by the PlayFab title. This network identifier includes the invitation id
, which we use as the invitation code to join a locked lobby, concatenated with the network descriptor.
Once the game has retrieved the network identifier executes the CreateMatchLobby
Azure Function, through the usage of PlayFab services. It is made using the CreateMatchLobby
method from the MatchLobbyHandler
. Internally, the MatchLobbyHandler
uses the PlayFabCloudScriptAPI.ExecuteFunction
from PlayFab SDK.
NOTE: To be able of creating a Party network the PlayFab title must have enabled the use of Party. Here is a guide about how do it.
PlayFab allows the integration with Azure Functions working as an intermediary between the Unity game and the Azure Function.
In this sample, PlayFab receives the request to execute the Azure Function with the name CreateMatchLobby
.
The Azure Function CreateMatchLobby
is responsible for creating the match lobby and storing it in the Cosmos DB.
This function receives as arguments the match lobby name, network id, and the locked state. Besides, it extracts from the request context the identifier of the player who has called the function to uses as the match lobby creator identifier.
NOTE: By default, the game always creates the lobby as locked.
With this data, plus the CurrentAvailability
set in 1, an instance of MatchLobby
class is created and inserted into the Cosmos DB.
Finally, the MatchLobby
is returned to the game which waits for the event OnRemotePlayersJoined
, that is triggered when the second player joins the match lobby network. In order that, the guest has to know the network identifier which is retrieved from the JoinMatchLobby
function, sending the lobby id and the invitation code if the lobby is locked. For more details you can see join document.
At that moment, the host game instance catches the OnRemotePlayersJoined
event and sends a Players Ready
message, which will trigger the set of ApplicationModel.StartMatch
property in true
in the guest game instance. Furthermore, it also set the same property with the same value in the host game.
This property will be evaluated in the next Update
Unity event execution in both host and guest instances of the game, and execute the StartMatch
method which will load the game scene.