Back To Home

Photon

16-04-2024

Room Service in PUN: Understanding Rooms and Concepts in (PUN):

"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."

What is a room ?

  1. A room is like a virtual space where players gather to play the game together.

  2. It's where the action happens, and players can interact with each other.

Think of Rooms Like Neighborhoods:

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.

  1. Join the Fun :
    You can let players join rooms that already exist. This is like moving into an established neighborhood.

  2. Start a New Adventure :
    Players can also create their own rooms, like founding a new club or setting up a custom game.

  3. 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.

  4. 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.

Creating a Room :

  1. To start a multiplayer game, someone needs to create a room.

  2. The creator of the room becomes the host, or "Master Client," who has special powers like starting or stopping the game.

Joining a Room :

  1. Players can join existing rooms to play with others.

  2. When you join a room, you become part of that game session and can interact with other players in the room.

Room Properties :

  1. Rooms can have custom properties associated with them, which provide additional information about the room's state or settings.

  2. Custom properties can be used for various purposes, such as indicating the game mode, level, or any other relevant information.

Room List :

  1. Photon provides a list of available rooms that players can browse.

  2. Players can choose a room from the list based on their preferences, such as the number of players or the game mode.

Lobby :

  1. A lobby is like a waiting area where players hang out before joining a room.

  2. It's where players can see the available rooms and decide where to play.

Room Events :

  1. Rooms have events that let players know what's happening, like when someone joins or leaves the room.

  2. These events help keep everyone in the room updated about the game's status.

Room Options :

  1. When creating a room, you can choose various options like the maximum number of players, whether the room is visible to others, etc.

  2. These options help customize the game experience to suit your needs.

Room Cleanup :

  1. It's essential to properly manage rooms to avoid clutter and ensure efficient use of resources.

  2. 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.

Lets understand Room properties in detail :

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:

Example Scenario :

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:

Choosing a Mode :

  1. Players join rooms based on the mode they want to play.

  2. Room properties show available modes, so players can pick one they like.

Starting the Game :

  1. When players join a room, the game sets up according to the chosen mode.

  2. For example, if it's "Capture the Flag," the game spawns flags; if it's "Team Deathmatch," it assigns teams.

Updates During Play :

  1. Room properties can change during the game, like adjusting settings.

  2. Players see these changes and adapt to the new game conditions.


Here's the example of how you might set and use room properties :

  
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
    }
}
  

In this example :

  1. When creating a room (CreateRoom method), we set the "Mode" room property to indicate the game mode chosen by the player.

  2. When joining a room (JoinRoom method), we don't set room properties, but we can access them after successfully joining in the OnJoinedRoom callback.

  3. 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.

  4. This is a basic example to illustrate how room properties can be used in code. Depending on your game's requirements

Here are a few more key points about rooms that you should know :

1.Room Visibility :

  1. Rooms can be either visible or invisible to other players in the lobby.

  2. Visible rooms are displayed in the room list, allowing players to see and join them.

  3. Invisible rooms are hidden from the room list and can only be joined by direct invitation or through custom matchmaking logic.

2.Room Capacity :

  1. Each room has a maximum capacity, which determines the maximum number of players allowed in the room.

  2. When creating a room, you can specify the maximum number of players allowed, and Photon ensures that the room does not exceed this capacity.

3.Room Name and ID :

  1. Rooms are identified by a unique name and ID within the Photon network.

  2. Room names can be chosen by players when creating rooms or generated automatically by Photon if not specified.

  3. Room IDs are unique identifiers assigned by Photon and are useful for referencing rooms programmatically.

4.Room Cleanup :

  1. It's essential to properly manage rooms to avoid clutter and ensure efficient use of resources.

  2. 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.

  3. 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!


More Blogs

See All