How to send complex data along with Events?

I want to send complex data along with event, such as List\Dictionary\Custom Class, are there any related docs? thanks!

Comments

  • I was suggested to used tokens. But it is said that tokens should only be used for a few actions here: https://forum.photonengine.com/discussion/comment/53249#Comment_53249 But I'm going to use event system as the main mechanism so there'll be lots of events with data to be transmitted in a game.
  • Hi @SPF ,

    Yes, they should be used with care, as depending on the size of your Token, they can block your package flow, for example. Tokes need to be serialized into one package entirely. Read more here: https://doc.photonengine.com/en-us/bolt/current/reference/boltpacket

    Also, Bolt offers other ways to communicate data, like States and Commands, so you need to consider that using only Events may not be the right approach for you.

    Do you have a special reason to use mainly events?

    --
    Ramon Melo
    Photon Bolt Team
  • @ramonmelo Thank you. I remembered we had a related discussion about a month ago. I would use Bolt to make a Hearthstone like game. There's no point using States and Commands in that game. Only events would suffice. I need to figure out the right way to send complex data along with events.

    If I want to send List\Dictionary\Custom Class with events, are there any ways other than tokens?
  • In addition you can use raw bytes of events to send complex data. You will need to handle the serialization and so on. For example: https://gist.github.com/herpdederp/bd7c4ff2b0d93498ff1b61bb9777b0c7
  • Thank you @stanchion , but this seems too complex for my programming skills. I'll look into tokens today if there's no other way.
  • @ramonmelo I'm trying to use tokens to transfer Lists and Dictionarys. It seems I could serialize and deserialize data to and from ByteArray. But I'm having troubles on how to do that. Do you have recommended documentations on this? Thank you!
  • Hello @SPF ,

    Unfortunately no, but you should be able to search for ways to serialize those types into a byte array and send them over.

    You can also use the current Packet API to serialized them too, of course, not directly, as there is no method for that. Take a look at the script below to get an idea:
    public class MyToken : IProtocolToken
    {
        public List<int> data = new List<int>();
    
        public void Read(UdpPacket packet)
        {
            data.Clear();
    
            if (packet.ReadBool()) // check if we have data to read
            {
                var total = packet.ReadInt(); // read the total number of items
    
                for (int i = 0; i < total; i++)
                {
                    data.Add(packet.ReadInt()); // read each item and store on the list
                }
            }
        }
    
        public void Write(UdpPacket packet)
        {
            var total = data.Count;
    
            if (packet.WriteBool(total > 0)) // Write bool to signal we have some data
            {
                packet.WriteInt(total); // write the number of items
    
                foreach (var item in data)
                {
                    packet.WriteInt(item); // write each item
                }
            }
        }
    }
    

    --
    Ramon Melo
    Photon Bolt Team
  • @ramonmelo Thank you! I came up my own version here: https://forum.photonengine.com/discussion/comment/53704#Comment_53704
    But yours seems better and cleaner.