CurrentRoom.SetCustomProperties not working as expected

Options
I have two players in a multiplayer game that will each have their own list of units and need to be aware of the other players list of units. So I created a system where a unit can be represented by a code and an x/y coordinate pair. Then I setup photon custom properties to send updates about the units at the end of each players turn.

So at the end of your turn this code runs in order to send your list of units to the opponent:

for (int i = 0; i < PlayerOneUnits.Length; i++)
{
if(PlayerOneUnits[i].IsDestroyed == false)
{
unitCount++;
p.Add("PlayerOneUnit" + i.ToString(), PlayerOneUnits[i].Type);
//p.Add("PlayerOneUnit" + i.ToString() + "Team", PlayerOneUnits[i].Team);
p.Add("PlayerOneUnit" + i.ToString() + "X", PlayerOneUnits[i].Hex.Column);
p.Add("PlayerOneUnit" + i.ToString() + "Y", PlayerOneUnits[i].Hex.Row);
}
}
And at the start of the other players turn this code runs to update their list your units:

for (int i = 0; i < counter; i++)
{
int UnitCode = (int)PhotonNetwork.CurrentRoom.CustomProperties["PlayerOneUnit" + i.ToString()];
//int UnitTeam = (int)PhotonNetwork.CurrentRoom.CustomProperties["PlayerOneUnit" + i.ToString() + "Team"];
int UnitX = (int)PhotonNetwork.CurrentRoom.CustomProperties["PlayerOneUnit" + i.ToString() + "X"];
int UnitY = (int)PhotonNetwork.CurrentRoom.CustomProperties["PlayerOneUnit" + i.ToString() + "Y"];
Unit u = new Unit(UnitCode, 1);
NM.UpdateTileUnits(u, UnitX, UnitY);
}

Now at the start of the game this works fine: I spawn in 6 units, I end my turn so I set the custom properties to represent my 6 units, then the other players turn starts and he gets the updated list of my units.

However, a couple turns into the game, if I mark the first 6 units as destroyed and spawn in 10 new units, I should only tell the other player about my 10 new units. When I trace the code, the first loop runs appropriately and I only send the 10 new units. HOWEVER, when the other players turn starts: the second loop runs for a count of 10 like it is supposed to, but it has a list that is the 6 old units and 4 of the new units.

How come when I run the first for loop a second time it doesn't replace the first 6 entries? Am I doing something wrong?

Comments

  • The first time the first for loop runs it produces this:

    PlayerOneUnit0 = 0
    PlayerOneUnit0X = 26
    PlayerOneUnit0Y = 47

    PlayerOneUnit1 = 0
    PlayerOneUnit1X = 24
    PlayerOneUnit1Y = 47

    PlayerOneUnit2 = 0
    PlayerOneUnit2X = 25
    PlayerOneUnit2Y = 46

    PlayerOneUnit3 = 0
    PlayerOneUnit3X = 25
    PlayerOneUnit3Y = 48

    PlayerOneUnit4 = 0
    PlayerOneUnit4X = 26
    PlayerOneUnit4Y = 46

    PlayerOneUnit5 = 0
    PlayerOneUnit5X = 24
    PlayerOneUnit5Y = 48

    The firs time the second for loop it reads this from the server:

    PlayerOneUnit0 = 0
    PlayerOneUnit0X = 26
    PlayerOneUnit0Y = 47

    PlayerOneUnit1 = 0
    PlayerOneUnit1X = 24
    PlayerOneUnit1Y = 47

    PlayerOneUnit2 = 0
    PlayerOneUnit2X = 25
    PlayerOneUnit2Y = 46

    PlayerOneUnit3 = 0
    PlayerOneUnit3X = 25
    PlayerOneUnit3Y = 48

    PlayerOneUnit4 = 0
    PlayerOneUnit4X = 26
    PlayerOneUnit4Y = 46

    PlayerOneUnit5 = 0
    PlayerOneUnit5X = 24
    PlayerOneUnit5Y = 48

    The second time the first for loop runs it produces this:

    PlayerOneUnit0 = 0
    PlayerOneUnit0X = 24
    PlayerOneUnit0Y = 46

    PlayerOneUnit1 = 0
    PlayerOneUnit1X = 24
    PlayerOneUnit1Y = 45

    PlayerOneUnit2 = 0
    PlayerOneUnit2X = 24
    PlayerOneUnit2Y = 44

    PlayerOneUnit3 = 0
    PlayerOneUnit3X = 24
    PlayerOneUnit3Y = 43

    PlayerOneUnit4 = 0
    PlayerOneUnit4X = 24
    PlayerOneUnit4Y = 42

    PlayerOneUnit5 = 0
    PlayerOneUnit5X = 23
    PlayerOneUnit5Y = 47

    PlayerOneUnit6 = 0
    PlayerOneUnit6X = 23
    PlayerOneUnit6Y = 46

    PlayerOneUnit7 = 0
    PlayerOneUnit7X = 23
    PlayerOneUnit7Y = 45

    PlayerOneUnit8 = 0
    PlayerOneUnit8X = 23
    PlayerOneUnit8Y = 44

    PlayerOneUnit9 = 0
    PlayerOneUnit9X = 24
    PlayerOneUnit9Y = 43

    However the second time the second for loop runs it reads this from the server:

    PlayerOneUnit0 = 0
    PlayerOneUnit0X = 26
    PlayerOneUnit0Y = 47

    PlayerOneUnit1 = 0
    PlayerOneUnit1X = 24
    PlayerOneUnit1Y = 47

    PlayerOneUnit2 = 0
    PlayerOneUnit2X = 25
    PlayerOneUnit2Y = 46

    PlayerOneUnit3 = 0
    PlayerOneUnit3X = 25
    PlayerOneUnit3Y = 48

    PlayerOneUnit4 = 0
    PlayerOneUnit4X = 26
    PlayerOneUnit4Y = 46

    PlayerOneUnit5 = 0
    PlayerOneUnit5X = 24
    PlayerOneUnit5Y = 48

    PlayerOneUnit6 = 0
    PlayerOneUnit6X = 23
    PlayerOneUnit6Y = 46

    PlayerOneUnit7 = 0
    PlayerOneUnit7X = 23
    PlayerOneUnit7Y = 45

    PlayerOneUnit8 = 0
    PlayerOneUnit8X = 23
    PlayerOneUnit8Y = 44

    PlayerOneUnit9 = 0
    PlayerOneUnit9X = 24
    PlayerOneUnit9Y = 43