Hey guys,
I’m having yet another UNET serialization issue. I’m currently making a networked CCG with UNET and am using custom messages for much of my communication.
However now one message is not returning the correct value from the NetworkReader.ReadInt32() method.
Here’s the code:
public void RequestSetDeck(List<Card> deck_, int playerID_)
{
the_client.Send(GameConfig.DeckNetworkMessage, new DeckMessage(deck_, playerID_));
}
private void SERVER_OnSetDeckRequestRecieved(NetworkMessage netMsg)
{
DeckMessage m = netMsg.ReadMessage<DeckMessage>();
Debug.Log(m.playerID);
Debug.Log(m.cards[29].id);
try
{
if (m.playerID == 0)
{
instance.the_match.player1.SetDeck(m.cards);
instance.RpcDeckSetConfirmed(m.playerID);
}
else if (m.playerID == 1)
{
instance.the_match.player2.SetDeck(m.cards);
instance.RpcDeckSetConfirmed(m.playerID);
}
}
catch (DeckInvalidException e)
{
instance.RpcDeckSetDenied(m.playerID);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
Debug.Log(m.playerID) writes 3474792 to the console log eventhough playerID in RequestSetDeck is 0. If I hardcode a number into the Serialize method in my DeckMessage class the Log still prints 3474792.
Debug.Log(Debug.Log(m.cards[29].id)) throws an IndexOutOfBounds Exception eventhough the list during serialization has 30 entries.
Here is my Message class:
public class DeckMessage : MessageBase
{
public List<Card> cards;
public int playerID;
public DeckMessage () { }
public DeckMessage(List<Card> data_, int playerID_)
{
cards = new List<Card>();
playerID = playerID_;
foreach (Card c in data_)
{
cards.Add(c);
}
}
public override void Serialize(NetworkWriter writer)
{
writer.StartMessage(GameConfig.DeckNetworkMessage);
writer.Write(playerID);
for (int i = 0; i < GameConfig.deckSize; i++)
{
if (i >= cards.Count || cards[i] == null)
{
writer.Write(false);
continue;
}
else
{
writer.Write(true);
cards[i].Serialize(writer);
}
}
writer.FinishMessage();
}
public override void Deserialize(NetworkReader reader)
{
this.cards = new List<Card>();
this.playerID = reader.ReadInt32();
for (int i = 0; i < GameConfig.deckSize; i++)
{
if (!reader.ReadBoolean())
{
continue;
}
else
{
cards.Add(Card.Deserialize(reader));
}
}
}
}
This pattern was working fine for other classes which had more data. Not sure what’s causing this. Any help would be appreciated!