I’m currently using Photon Network and I need to send an RPC to the connected players with the teams. I created a Team object that defines a team, and all the teams are stored in an array.
Since arrays and custom objects are not serialized automatically, how can I do it? I have no experience in this field and I have googled a lot with no sucess. All I need is some directions on how to do it.
P.S- Actually I read something about converting it all to a byte array an then send it. Any guidelines on how to do this?
Here is my Team object:
public class Team {
private int teamID;
private string[] players;
private int playersJoined;
}
EDIT - I folowed @ArkaneX sugestion and got this:
private static byte[] serializeTeam (object o)
{
Team team = (Team)o;
byte[] bytes = new byte[3 * 4];
int index = 0;
ExitGames.Client.Photon.Protocol.Serialize(team.getID(), bytes, ref index);
ExitGames.Client.Photon.Protocol.Serialize(team.getPlayerList(), bytes, ref index);
ExitGames.Client.Photon.Protocol.Serialize(team.getSize(), bytes, ref index);
return bytes;
}
private static object deserializeTeam(byte[] bytes)
{
Team team = new Team();
int index = 0;
ExitGames.Client.Photon.Protocol.Deserialize(out team.teamID, bytes, ref index);
ExitGames.Client.Photon.Protocol.Deserialize(out team.players, bytes, ref index);
ExitGames.Client.Photon.Protocol.Deserialize(out team.playersJoined, bytes, ref index);
return team;
}
I tought that arrays of strings would spare me these troubles but I’m getting this:
Argument `#1' cannot convert `string[]' expression to type `short'
Also I’m not sure about bytes array length.
EDIT 2 - I have this another idea, but the same error
private static byte[] serializeTeam (object o)
{
Team team = (Team)o;
byte[] bytes = new byte[3 * 4];
int index = 0;
ExitGames.Client.Photon.Protocol.Serialize(team.teamID, bytes, ref index);
ExitGames.Client.Photon.Protocol.Serialize(team.players.Length, bytes, ref index);
foreach(string s in team.players)
{
ExitGames.Client.Photon.Protocol.Serialize(s, bytes, ref index);
}
ExitGames.Client.Photon.Protocol.Serialize(team.playersJoined, bytes, ref index);
return bytes;
}
private static object deserializeTeam(byte[] bytes)
{
Team team = new Team();
int index = 0;
ExitGames.Client.Photon.Protocol.Deserialize(out team.teamID, bytes, ref index);
int lenght = 0;
ExitGames.Client.Photon.Protocol.Deserialize(out lenght, bytes, ref index);
team.players = new string[lenght];
for (int i = 0; i < team.playersJoined; ++i)
{
ExitGames.Client.Photon.Protocol.Deserialize(out team.players*, bytes, ref index);*
-
}*
-
ExitGames.Client.Photon.Protocol.Deserialize(out team.playersJoined, bytes, ref index);*
-
return team;*
- }*