Ideas for an event handler (Unity)


I've been playing around with PTB for awhile, implementing some simple features into my game. Being a turn-based game I've come to realize that I need some system in place to control when and how photon events are dealt with. For instance if a client is loading a scene or joining an ongoing game, I need to take that into account and deal with those events in a different way, I probably need to wait for the scene to load in the first case and quickly synchronize the game via the cached events in the second skipping any animation etc.

I'm proposing an idea here for how I might do such a thing in case anyone has implemented something similar or given it some thought, and might be able to give me some advice on what might/might not work. I was also thinking that this behavior might already be incorporated somehow but I've just missed it :D.

I'm thinking of having a class lets call it:


Queue<EventData> photonEventQueue //store events in a queue so that they maintain their order

//I'll just put all the functionality I'm thinking of as methods for now ofc that's not how it would work

void AddEvent(EventData event) //called by existing OnEvent from class extending LoadBalancingClient



ProcessSpecificEvents(byte eventCode)

StopProcessingSpecificEvents(byte eventCode)

ProcessUntilEvent(byte eventCode)

If possible let me know if you think this sort of thing is a good/bad idea and how you might use/implement it.


  • You are on a good track.

    You should be able to leverage that the lower level library queues and sorts incoming messages until you dispatch them.
    So to stop executing events on demand, you can modify your code to pause PhotonPeer.DispatchIncomingCommands.
    You will have to make sure that SendOutgoingCommands is still called, even while you load (then Update() is not called). Take a look at PhotonPeer.SendAcksOnly(). It can be called when you don't need/want to send operations to the server (and anyone else) but need to keep connection.
  • Many thanks,

    I had a feeling there would already be something I could use. If I manage anything I feel might be useful for others I shall post it here.

    One other thing, is there any way to determine whether or not an event was sent via the cache or a user?
  • You can't find out if an event is from the cache or not. You get them before you get any "live" events but you can't really differentiate.
    In worst case, make some other player send an event "i saw your join-event" to a joining player. When that's executed, you are done with the buffered ones.
  • JohnTube
    JohnTube ✭✭✭✭✭
    v1r7u3 wrote:
    If I manage anything I feel might be useful for others I shall post it here.

    Can you show us some code ?