Home > Back-end >  How to get or share some data from a client in a lobby to others over a network before they join the
How to get or share some data from a client in a lobby to others over a network before they join the

Time:03-23

I am new to PUN2. I am facing an issue that I want to describe some betting amount for joining my room which I define at the time of room creation. The issue is that I want that amount must be shown to others when they are in a lobby, from where they could decide whether they join/not by paying that amount. For this I am doing: I have 1 MenuManager.cs script in which

public InputField amount;
[SerializeField] Transform _content; //container of list
[SerializeField] RoomListing _roomListing;
List<RoomListing> _listings = new List<RoomListing>();

public int SetBetAmount()
{
    if (string.IsNullOrEmpty(amount.text))
    {

        return -1;
    }
    else
        return (int.Parse(amount.text));

}

// The Script that runs when Create Room Button Clicks as follow:

public void OnCreateRoomBtnClicked()
    {
        string roomName = "Room "   Random.Range(1, 800);
        int maxPlayersInt = SetMaxPlayers();
        RoomOptions roomOptions = new RoomOptions();
        roomOptions.MaxPlayers = (byte)maxPlayersInt;
        string[] roomPropertiesInLobbby = { "betAmount" };
        betAmount = SetBetAmount();
        //customRoomProps["betAmount"] = (byte)SetBetAmount();
        Debug.Log("Bet Amount Updated"   customRoomProps["betAmount"]);
        SetLaps();
        roomOptions.CustomRoomPropertiesForLobby = roomPropertiesInLobbby;
        roomOptions.CustomRoomProperties = customRoomProps;
        PhotonNetwork.CreateRoom(roomName, roomOptions);
    }

OnRoomListUpdate Callback works fine for info data but not sending correct betAmount but the garbage value 0;

 public override void OnRoomListUpdate(List<RoomInfo> roomList)
    {
        foreach (RoomInfo info in roomList)
        {
            if (info.RemovedFromList)
            {
                int index = _listings.FindIndex(x => x.RoomInfo.Name == info.Name);
                if (index != -1)
                {
                    Destroy(_listings[index].gameObject);
                    _listings.RemoveAt(index);
                }
            }

        else
        {
            RoomListing listing = Instantiate(_roomListing, _content);
            if (listing != null)
            {
                listing.GetComponent<RoomListing>().bettingAmt = -3; //there I tried betAmount but it sends 0
                listing.SetRoomInfo(info);
                _listings.Add(listing);
            }
        }
    }
}

I have also tried this but not sure how to do this

 public override void OnJoinedLobby()
    {
        //if (customRoomProps.ContainsKey("betAmount"))
        //{
        //    //Debug.Log("Call From Master");
        //    object _amount;
        //    if (customRoomProps.TryGetValue("betAmount", out _amount))
        //    {

        //        Debug.Log("Bet Amount"   _amount.ToString());
        //        betAmountS = (int)_amount;
        //        Debug.Log("BetAmount  "   betAmount);
        //    }
        //}

        //else
        //{
        //    Debug.Log("Call From local");
        //}
    }

Plus, I have also tried PUNRPC but it works when others join the Room then they could see that data.

CodePudding user response:

I don't have your entire code so I can only suspect what happens but my guess is the following:

The key is definitely added to the room properties otherwise it wouldn't return 0 but throw a KeyNotFoundException.

So from your given code I guess you somewhere have a predefined

private Hashtable customRoomProps = new Hashtable ();
private int betAmount;

and somewhere already set

customRoomProps ["betAmount"] = betAmount;

now when doing

betAmount = SetBetAmount();

you expect that the value is also updated within the customRoomProps.

BUT int is a value-type! The value you stored in the customRoomProps is a copy by value of whatever value was currently stored in betAmount the moment you added it.

The value stored in customRoomProps is in no way related to the betAmount anymore!

You rather need to set the value the moment you need it

betAmount = SetBetAmount();
customRoomProps["betAmount"] = betAmount;

basically the line you have commented out.


And then in OnRoomListUpdate you should be able to get it like

if(info.CustomProperties.TryGetValue("betAmount", out var betAmount))
{
    listing.GetComponent<RoomListing>().bettingAmt = (int) betAmount;
}
  • Related