Home > OS >  How not to join private room or room with bet amount on PhotonNetwork.JoinRandomRoom() function call
How not to join private room or room with bet amount on PhotonNetwork.JoinRandomRoom() function call

Time:03-29

I am developing a multiplayer racing game in unity using PUN2. I want to create 2 types of rooms. The first type is that players create a room with some constraints that max players, no of laps and bet amount (that may be called a private room or price to pay to join the room). And 2nd type is the free room type. For this, I've used JoinRandomRoom() function. So the issue is that when I use the JoinRandomRoom() function it joins the First type of room (without even paying the price). Now I want to define constraints for this type of room is that when the JoinRandoomRoom() function calls it must not be allowed to join the first type of room if there is no free room available it creates a new room.

I have found 2 overloads of JoinRandomRoom() function but couldn't understand how to use them. One thing I also found that while creating the First type of room I could add some additional strings like the word "Private" in starting of the Room name so that I can keep check of such rooms but didn't get how to pass such conditions to JoinRandomRoom() function that if the room name starts with the word private do not join that room and create a new one.

CodePudding user response:

You can filter rooms for JoinRandomRoom!

In your use case I would simply introduce a custom property - make sure it is also exposed in the lobby - called e.g. RANDOM which you set only in the random rooms, but not in the private or "paid" rooms.

=> you can join the private and paid rooms explicitly but only those that have the RANDOM property key set via JoinRandomRoom.

Then you can simply filter somewhat like e.g.

private const string RANDOM_PROP_KEY = "RANDOM";
private const byte RANDOM_PROP_VALUE = 42;

private readonly Hashtable randomCustomRoomProperties = new Hashtable { { RANDOM_PROP_KEY, RANDOM_PROP_VALUE  } };
private readonly string [] exposedProperties = new []{ RANDOM_PROP_KEY };

private readonly RoomOptions randomRoomOptions = new RoomOptions
{  
    customRoomProperties = randomCustomRoomProperties,
    customRoomPropertiesForLobby = exposedProperties,
    // ...
};

public void JoinRandomRoom()
{  
    // filter using the expected properties and 0 for ANY max player amount
    PhotonNetwork.JoinRandomRoom(expectedCustomRoomProperties, 0););
}

void IMatchmakingCallbacks.OnJoinRandomFailed(short returnCode, string message)
{
    // No room found matching the filters -> create new
    Debug.LogWarning($"Join random failed with {returnCode} - \"{message}\"\n\n->Instead creating a new random room");
    // Create room using the random properties and NULL for the name so Photon creates a random GUID
    PhotonNetwork.CreateRoom(null, randomRoomOptions);
}

void IMatchmakingCallbacks.OnJoinedRoom()
{
    // joined a room successfully

    // Could be both, random or paid => check if has cost here
}
  • Related