Search Unity

Resolved How to store and modify variables in the photon PUN system?

Discussion in 'Lobby' started by OogaPooga, Jul 2, 2022.

Thread Status:
Not open for further replies.
  1. OogaPooga

    OogaPooga

    Joined:
    Feb 23, 2022
    Posts:
    2
    I am making a 2D top down multiplayer game, and am using the photon unity networking. I have built a system that stores information about the health of my Tilemap(each tile has an assigned HP value) in a 2d array, however I would like to store this information on a server, so it can be accessed and changed by the players. how would I do this?

    Code (CSharp):
    1. private float[,] locHP = new float[mapSize, mapSize];
    2.    public void Start(){
    3.            destructibleTileMap = GetComponent<Tilemap>();
    4.            for(int x = 0; x<=mapSize; x++)
    5.            {
    6.                for(int y = 0; y<= mapSize; y++)
    7.                {
    8.                    Vector3Int pos1 = new Vector3Int(x, y, 0);
    9.                    if (destructibleTileMap.HasTile(pos1)) {
    10.                    string hp = destructibleTileMap.GetTile(pos1).name;
    11.                    float health = float.Parse(hp.Substring(hp.Length-3));
    12.  
    13.                    locHP[x,y] = health;//I want to store and modify this variable in the cloud
    14.                    }
    15.                }
    16.            }
    17.        }
     
  2. veleek_unity

    veleek_unity

    Ben Randall Unity Technologies

    Joined:
    Aug 25, 2021
    Posts:
    59
    This forum is for the new Unity Gaming Services (UGS) Lobby service - https://docs.unity.com/lobby
    You might be looking for more information related to the Photon lobby functionality (https://doc.photonengine.com/en-us/pun/current/demos-and-tutorials/pun-basics-tutorial/lobby) or some other detail about Photon.

    Photon is not owned or operated by Unity so unfortunately we don't have the info you're looking for. Check out the Photon forums here instead: https://forum.photonengine.com/sso


    That being said, if you decide to consider UGS there are some options. It kind of depends on how this data is changed. The Lobby service is used to get players together in groups (by providing the ability to search) and help coordinate having them connect using some other mechanism. It's not really designed to synchronize real-time game data. The general rule of thumb is that you should store data in a Lobby if it's something that you want to be able to search on (e.g. Find all lobbies with mapType="forest").

    However, that doesn't mean that you can't use Lobby. If you game is turn-based and it's okay if there's a bit of a delay before all the other clients get the update then you could use Lobby. On the other hand, if you want the data to be updated in real-time, you would likely connect all players together Relay service and have one player be the host to manage the data and updated it and notify other players of changes. If you want the data to be stored securely outside of one of the players game clients, you would need to use something like a dedicated game server, or store the data in Cloud Save and read/write it each time.
     
  3. OogaPooga

    OogaPooga

    Joined:
    Feb 23, 2022
    Posts:
    2
    so, thanks to @veleek_unity for the suggestion, however I did find a solution. I used the isWriting and isReading to transfer the information.
    Code (CSharp):
    1. public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    2.     {
    3.         if (stream.IsWriting)
    4.         {
    5.     // We own this: send the others our data
    6.                 stream.SendNext(hitPointA);
    7.                 stream.SendNext(hitPointB);
    8.                 stream.SendNext(hitPointDamage);
    9.                 hitPointDamage = 0;
    10.         }
    11.         else
    12.         {
    13.                
    14.                 this.hitPointA = (float)stream.ReceiveNext();
    15.                 this.hitPointB = (float)stream.ReceiveNext();
    16.                 this.hitPointDamage = (float)stream.ReceiveNext();
    17.                 Vector2 hp = new Vector2(hitPointA, hitPointB);
    18.                 wallHealth.TakeDamage(hitPointDamage, hp, false);
    19.                 objectHealth.TakeDamage(hitPointDamage, hp, false);
    20.         }
    21.  
    22.  
    23.     }
    24.  
    25.     public void SendDamage(float hitA, float hitB, float hitDamage)
    26.     {
    27.         hitPointA = hitA;
    28.         hitPointB = hitB;
    29.         hitPointDamage = hitDamage;
    30.     }
    basically, every player is constantly sending three variables, hitPointA and hitPointB, both of which are coordinates, and damage. every time I need to send damage for the walls to the other players, just call SendDamage(x, y, damage) from another script, and it will send the info to all other players, and the walls on their computers will receive it and calculate the proper action. I believe that if there are more than two players, this system will have problems with priority, so i will have to later exchange each of the variables for arrays of size players. this system will also not transfer the current map status to players that have joined after some players have already damaged some walls. this will not effect my game, however for any future readers, that could be solvedby having the master client send all of the map data to any players that join during a game.
     
Thread Status:
Not open for further replies.