The PlayFlow SDK provides a comprehensive suite of tools for managing multiplayer lobbies, primarily through the PlayFlowLobbyManagerV2
component.
PlayFlowLobbyManagerV2
This is a high-level manager that you add to your scene. It handles session management, state synchronization, and provides a clean API for all lobby operations.
Initialization
Before using the manager, you must initialize it with a unique ID for the current player. This is typically done once when your game starts.
using PlayFlow;
using System;
public class MyGameManager : MonoBehaviour
{
void Start()
{
// It's a good practice to generate a unique ID for each player
string playerId = "player-" + Guid.NewGuid().ToString("N").Substring(0, 8);
// Initialize the manager
PlayFlowLobbyManagerV2.Instance.Initialize(playerId, OnManagerReady);
}
void OnManagerReady()
{
Debug.Log("PlayFlow Lobby Manager is ready!");
// You can now safely call other lobby methods
}
}
Creating a Lobby
Creating a lobby is simple. You can specify a name, max players, and whether it’s private.
public void CreateMyLobby()
{
PlayFlowLobbyManagerV2.Instance.CreateLobby(
name: "My Awesome Game",
maxPlayers: 8,
isPrivate: false,
onSuccess: (lobby) => {
Debug.Log($"Successfully created lobby: {lobby.name} (ID: {lobby.id})");
if (lobby.isPrivate)
{
Debug.Log($"Invite code: {lobby.inviteCode}");
}
},
onError: (error) => {
Debug.LogError($"Failed to create lobby: {error}");
}
);
}
Finding and Joining Lobbies
You can get a list of available public lobbies and then join one by its ID.
public void JoinFirstAvailableLobby()
{
PlayFlowLobbyManagerV2.Instance.GetAvailableLobbies(
onSuccess: (lobbies) => {
var availableLobby = lobbies.Find(l => !l.isPrivate && l.currentPlayers < l.maxPlayers);
if (availableLobby != null)
{
PlayFlowLobbyManagerV2.Instance.JoinLobby(availableLobby.id,
onSuccess: (lobby) => {
Debug.Log($"Successfully joined lobby: {lobby.name}");
},
onError: (error) => {
Debug.LogError($"Failed to join lobby: {error}");
}
);
}
else
{
Debug.LogWarning("No available public lobbies found.");
}
},
onError: (error) => {
Debug.LogError($"Failed to get lobbies: {error}");
}
);
}
To join a private lobby, use JoinLobbyByCode(code, ...)
.
Handling Events
The SDK uses UnityEvent
s to notify your game of changes. You can subscribe to these to update your UI or game state.
void SubscribeToEvents()
{
var events = PlayFlowLobbyManagerV2.Instance.Events;
// Fired when the local player joins a lobby
events.OnLobbyJoined.AddListener(OnLobbyJoined);
// Fired when another player joins the lobby
events.OnPlayerJoined.AddListener(OnPlayerJoined);
// Fired when the lobby data is updated
events.OnLobbyUpdated.AddListener(OnLobbyUpdated);
}
void OnLobbyJoined(Lobby lobby)
{
Debug.Log($"Joined lobby: {lobby.name}");
}
void OnPlayerJoined(PlayerAction action)
{
Debug.Log($"Player {action.PlayerId} joined the lobby!");
}
void OnLobbyUpdated(Lobby lobby)
{
Debug.Log("Lobby was updated.");
// Re-draw your UI here
}
Responses are generated using AI and may contain mistakes.