How to sync a list?

I have an object in my scene which stores everything about a match: duration, scores, players, etc. what i want to do is implement a scoreboard, and i do this by accessing the list of red players and blue players. the problem is, this list doesnt seem to be syncing. can someone help me? here is what ive tried:
 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		else if (stream.isReading)
		{
			numOfPlayers = (int)stream.ReceiveNext();
			maxPlayers = (byte)stream.ReceiveNext();
			BlueTeam = (List<GameObject>)stream.ReceiveNext();
			RedTeam = (List<GameObject>)stream.ReceiveNext();
			players = (List<GameObject>)stream.ReceiveNext();
			bluespawnnum = (int)stream.ReceiveNext();
			redspawnnum = (int)stream.ReceiveNext();
		}
	}

Answers

  • Hi @LeytonMate,

    you can't serialize a List directly and would have to register a Custom Type for it. Since this requires some effort to implement, I would advise you to use an Array instead. Since you already have a List you can use it's ToArray() function to cast the List into an Array.

    This however won't work either in your case, because your List consists of GameObjects, which aren't serializable by default as well. So instead of trying to synchronize this list, you can try to use the implementations PUN already provides. There are for example functionalities for Teams and Player Scores, which are automatically synchronized across all clients.
  • LeytonMate
    edited July 2018

    Hi @LeytonMate,

    you can't serialize a List directly and would have to register a Custom Type for it. Since this requires some effort to implement, I would advise you to use an Array instead. Since you already have a List you can use it's ToArray() function to cast the List into an Array.

    This however won't work either in your case, because your List consists of GameObjects, which aren't serializable by default as well. So instead of trying to synchronize this list, you can try to use the implementations PUN already provides. There are for example functionalities for Teams and Player Scores, which are automatically synchronized across all clients.

    ok, im setting up the teams and ive run into a problem. when i enter a room, it compares the count of the teams and puts the player on the team with less members. how do i set a list equal to the photon red team? here is what i tried to do:

    RedTeam = FindObjectsOfType<PunTeams>().Where(x => x.team == 1).Select(s => s.gameObject).ToList();

  • You just need to add the PunTeams component to a GameObject in the scene. Afterwards you can use int blueTeam = PunTeams.PlayersPerTeam[PunTeams.Team.blue].Count; for example to get the number of players that have joined the Blue team. To get all players of a certain team you can use List<PhotonPlayer> blueTeam = PunTeams.PlayersPerTeam[PunTeams.Team.blue]; to get the Blue team in this case.