How to sync a list?

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to sync a list?

LeytonMate
2018-07-10 16:17:32

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)stream.ReceiveNext();  
			RedTeam = (List)stream.ReceiveNext();  
			players = (List)stream.ReceiveNext();  
			bluespawnnum = (int)stream.ReceiveNext();  
			redspawnnum = (int)stream.ReceiveNext();  
		}  
	}

Comments

[Deleted User]
2018-07-12 09:57:19

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
2018-07-13 17:27:47

@Christian_Simon wrote:

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().Where(x => x.team == 1).Select(s => s.gameObject).ToList();

[Deleted User]
2018-07-17 08:18:48

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 blueTeam = PunTeams.PlayersPerTeam[PunTeams.Team.blue]; to get the Blue team in this case.

Back to top