How to read and reset a pooled token in a pooled token?

Options
SPF
SPF
edited December 2020 in Photon Bolt
Example token:
public class DrawCardEventToken : PooledProtocolToken
{
    public byte actorID;
    public Card card;
    public override void Read(UdpPacket packet)
    {
        actorID = packet.ReadByte();
        card = packet.ReadToken() as Card; // need help here
    }

    public override void Write(UdpPacket packet)
    {
        packet.WriteByte(actorID);
        packet.WriteToken(card);
    }

    public override void Reset()
    {
        // and here
        throw new System.NotImplementedException();
    }
}

The Card is a pooled token:
public class Card: PooledProtocolToken
{
    public int id;
    public int key;
    public int cost;
    public int attack;
    public int health;
   

    public override void Read(UdpPacket packet)
    {
        id = packet.ReadInt();
        key = packet.ReadInt();
        cost = packet.ReadInt();
        attack = packet.ReadInt();
        health = packet.ReadInt();
    }

    public override void Write(UdpPacket packet)
    {
        packet.WriteInt(id);
        packet.WriteInt(key);
        packet.WriteInt(cost);
        packet.WriteInt(attack);
        packet.WriteInt(health);
    }

    public override void Reset()
    {
        id = default(int);
        key = default(int);
        cost = default(int);
        attack = default(int);
        health = default(int);
    }
   
    static public Card NewCard(int key, int id)
    {
        var card = ProtocolTokenUtils.GetToken<Card>();
        card.key = key;
        card.id = id;
        var settingData = SettingDataMgr.Instance.GetCardSettingData(key);
        card.cost = settingData.cost;
        card.attack = settingData.attack;
        card.health = settingData.health;
        return card;
    }
}

I tried to change the "Card" from "PooledProtocolToken" to "IProtocolToken", but it raised two errors in testing.
Error messages one:
User code threw exception when invoking [DrawCardEvent DrawCardEventToken=]
UnityEngine.Debug:LogError(Object)
UnityWriter:BoltLog.IWriter.Error(String)
BoltLog:Error(String)
BoltLog:Error(String, Object)
Bolt.EventDispatcher:Raise(Event)
Bolt.EventDispatcher:RaiseLocal(Event)
Bolt.EventDispatcher:Global_Everyone(Event)
Bolt.EventDispatcher:Dispatch(Event)
Bolt.EventDispatcher:DispatchAllEvents()
BoltInternal.BoltCore:Poll()
BoltPoll:FixedUpdate()
Followed by error two:
NullReferenceException: Object reference not set to an instance of an object
Battle.OnEventDraw (DrawCardEventToken token) (at Assets/_Scripts/Battle/Battle.cs:193)
NetworkCallbacks.OnEvent (DrawCardEvent evnt) (at Assets/_Scripts/NetworkCallbacks.cs:84)
DrawCardEvent_Meta.Bolt.IEventFactory.Dispatch (Bolt.Event ev, System.Object target) (at Temp/bolt/project.cs:851)
Bolt.EventDispatcher.Raise (Bolt.Event ev) (at <ed5388b742a94b18b9b7f386d8d06b96>:0)
UnityEngine.Debug:LogException(Exception)
BoltLog:Exception(Exception)
Bolt.EventDispatcher:Raise(Event)
Bolt.EventDispatcher:RaiseLocal(Event)
Bolt.EventDispatcher:Global_Everyone(Event)
Bolt.EventDispatcher:Dispatch(Event)
Bolt.EventDispatcher:DispatchAllEvents()
BoltInternal.BoltCore:Poll()
BoltPoll:FixedUpdate()

These two errors appeared after a message that might help:
Raising [DrawCardEvent DrawCardEventToken=]
UnityEngine.Debug:Log(Object)
UnityWriter:BoltLog.IWriter.Debug(String)
BoltLog:Debug(String)
BoltLog:Debug(String, Object)
Bolt.EventDispatcher:RaiseLocal(Event)
Bolt.EventDispatcher:Global_Everyone(Event)
Bolt.EventDispatcher:Dispatch(Event)
Bolt.EventDispatcher:DispatchAllEvents()
BoltInternal.BoltCore:Poll()
BoltPoll:FixedUpdate()

Comments

  • SPF
    Options
    I tried to change the "Card" from "PooledProtocolToken" to "IProtocolToken", but it raised two errors:
    User code threw exception when invoking [DrawCardEvent DrawCardEventToken=]
    UnityEngine.Debug:LogError(Object)
    UnityWriter:BoltLog.IWriter.Error(String)
    BoltLog:Error(String)
    BoltLog:Error(String, Object)
    Bolt.EventDispatcher:Raise(Event)
    Bolt.EventDispatcher:RaiseLocal(Event)
    Bolt.EventDispatcher:Global_Everyone(Event)
    Bolt.EventDispatcher:Dispatch(Event)
    Bolt.EventDispatcher:DispatchAllEvents()
    BoltInternal.BoltCore:Poll()
    BoltPoll:FixedUpdate()

    Followed by:
    NullReferenceException: Object reference not set to an instance of an object
    Battle.OnEventDraw (DrawCardEventToken token) (at Assets/_Scripts/Battle/Battle.cs:193)
    NetworkCallbacks.OnEvent (DrawCardEvent evnt) (at Assets/_Scripts/NetworkCallbacks.cs:84)
    DrawCardEvent_Meta.Bolt.IEventFactory.Dispatch (Bolt.Event ev, System.Object target) (at Temp/bolt/project.cs:851)
    Bolt.EventDispatcher.Raise (Bolt.Event ev) (at <ed5388b742a94b18b9b7f386d8d06b96>:0)
    UnityEngine.Debug:LogException(Exception)
    BoltLog:Exception(Exception)
    Bolt.EventDispatcher:Raise(Event)
    Bolt.EventDispatcher:RaiseLocal(Event)
    Bolt.EventDispatcher:Global_Everyone(Event)
    Bolt.EventDispatcher:Dispatch(Event)
    Bolt.EventDispatcher:DispatchAllEvents()
    BoltInternal.BoltCore:Poll()
    BoltPoll:FixedUpdate()
  • ramonmelo
    Options
    Hello @SPF ,

    Currently, there is no support for using Pooled Tokens inside another Token, so, use the default implementation of the "IProtocolToken" in this case.

    You are receiving a "NullReferenceException" on your code, check which reference is missing and see where it should be filled. You need to track it down to where it's missing.

    --
    Ramon Melo
    Photon Bolt Team