Back To Home
Photon
16-04-2024
"This is a continuation of part 3. This post delves into the concepts of Rooms in (PUN). We'll explore how to handle rooms in PUN."
A room is like a virtual space where players gather to play the game together.
It's where the action happens, and players can interact with each other.
Imagine your multiplayer game as a big city. Rooms in PUN act like individual neighborhoods within that city. Players gather in specific rooms to play together, like hanging out in a specific park or joining a sports team.
Join the Fun :
You can let players join rooms that already exist. This is like moving into an established neighborhood.
Start a New Adventure :
Players can also create their own rooms, like founding a new club or setting up a custom game.
Room Rules :
Each room can have its own settings, like how many players can join, what kind of game it's for, or even where players are located in the world.
Stay Updated :
When something happens in a room, like a player joining or leaving, everyone in that room gets notified. This keeps everyone on the same page.
To start a multiplayer game, someone needs to create a room.
The creator of the room becomes the host, or "Master Client," who has special powers like starting or stopping the game.
Players can join existing rooms to play with others.
When you join a room, you become part of that game session and can interact with other players in the room.
Rooms can have custom properties associated with them, which provide additional information about the room's state or settings.
Custom properties can be used for various purposes, such as indicating the game mode, level, or any other relevant information.
Photon provides a list of available rooms that players can browse.
Players can choose a room from the list based on their preferences, such as the number of players or the game mode.
A lobby is like a waiting area where players hang out before joining a room.
It's where players can see the available rooms and decide where to play.
Rooms have events that let players know what's happening, like when someone joins or leaves the room.
These events help keep everyone in the room updated about the game's status.
When creating a room, you can choose various options like the maximum number of players, whether the room is visible to others, etc.
These options help customize the game experience to suit your needs.
It's essential to properly manage rooms to avoid clutter and ensure efficient use of resources.
Rooms should be closed or cleaned up when they are no longer needed, such as when all players have left or when the game session has ended.
Room properties are key-value pairs that provide additional information about the state or settings of a room. They can be used for various purposes in a multiplayer game to customize gameplay, manage game sessions, and communicate important information to players. Here's how room properties are typically used in a game:
Imagine you're creating a multiplayer shooter game where players can choose between different game modes, such as "Team Deathmatch" and "Capture the Flag." You want to use room properties to indicate the selected game mode and other relevant settings for each room.
In a multiplayer shooter game, players can choose different game modes like "Team Deathmatch" or "Capture the Flag." Room properties help indicate the selected mode:
Players join rooms based on the mode they want to play.
Room properties show available modes, so players can pick one they like.
When players join a room, the game sets up according to the chosen mode.
For example, if it's "Capture the Flag," the game spawns flags; if it's "Team Deathmatch," it assigns teams.
Room properties can change during the game, like adjusting settings.
Players see these changes and adapt to the new game conditions.
using UnityEngine;
using Photon.Pun;
public class GameManager : MonoBehaviourPunCallbacks
{
// Method to create a room with specified properties
public void CreateRoom(string mode)
{
RoomOptions options = new RoomOptions();
options.MaxPlayers = 4;
// Set custom properties for the room
ExitGames.Client.Photon.Hashtable customProps = new ExitGames.Client.Photon.Hashtable();
customProps["Mode"] = mode; // Set the game mode
options.CustomRoomProperties = customProps;
options.CustomRoomPropertiesForLobby = new string[] { "Mode" }; // Make the game mode visible in the lobby
// Create the room with the specified options
PhotonNetwork.CreateRoom(null, options);
}
// Method to join an existing room
public void JoinRoom()
{
PhotonNetwork.JoinRandomRoom(); // Join a random room
}
// Callback triggered when successfully joined a room
public override void OnJoinedRoom()
{
// Access room properties
string mode = (string)PhotonNetwork.CurrentRoom.CustomProperties["Mode"];
Debug.Log("Joined room with mode: " + mode);
// Perform other setup based on room properties
}
}
When creating a room (CreateRoom method), we set the "Mode" room property to indicate the game mode chosen by the player.
When joining a room (JoinRoom method), we don't set room properties, but we can access them after successfully joining in the OnJoinedRoom callback.
Inside OnJoinedRoom, we access the "Mode" property of the current room's custom properties and perform any necessary setup based on the chosen game mode.
This is a basic example to illustrate how room properties can be used in code. Depending on your game's requirements
Rooms can be either visible or invisible to other players in the lobby.
Visible rooms are displayed in the room list, allowing players to see and join them.
Invisible rooms are hidden from the room list and can only be joined by direct invitation or through custom matchmaking logic.
Each room has a maximum capacity, which determines the maximum number of players allowed in the room.
When creating a room, you can specify the maximum number of players allowed, and Photon ensures that the room does not exceed this capacity.
Rooms are identified by a unique name and ID within the Photon network.
Room names can be chosen by players when creating rooms or generated automatically by Photon if not specified.
Room IDs are unique identifiers assigned by Photon and are useful for referencing rooms programmatically.
It's essential to properly manage rooms to avoid clutter and ensure efficient use of resources.
Rooms should be closed or cleaned up when they are no longer needed, such as when all players have left or when the game session has ended.
Photon provides callbacks for handling room events, such as when the last player leaves the room or when the room is closed, allowing developers to perform cleanup tasks accordingly.
I hope you found this blog post enjoyable!