Search Unity

Best way to cache World snapshots?

Discussion in 'Entity Component System' started by slims, Sep 26, 2020.

  1. slims

    slims

    Joined:
    Dec 31, 2013
    Posts:
    86
    I'm implementing networking for my game and I need to be able to store N number of previous world states to rewind for lag compensation.

    My deterministic game simulation runs at 20 ticks per second, and I want to store ~5 ticks worth of states, so that if a client changes the state, other clients can rewind back to the tick they changed the state, and replay with the change.

    I can't find an obvious way to deep copy the world state on each tick, aside from serializing the World using the SerializationUtility, but it seems like serialization might be an additional step I don't need.

    It'd be great if there was a copy constructor and I could just do:

    Code (CSharp):
    1. var cachedWorld = new World(World.DefaultGameObjectInjectionWorld);
    2. snapshots.Add(cachedWorld);

    Then when I get an RPC that's going to change the state, the RPC will specify what tick the change was made at, I can grab the corresponding world from my list of snapshots, apply the change, make it the default world, and run the update loop on my systems for the number of ticks to catch up to where the simulation should currently be at.

    Is using the SerializationUtility just the way to go in lieu of a deep copy? Or am I thinking about this all wrong?

    (Note: I'm not using the new DOTS networking package for various reasons; I'm using Photon PUN2 for connections and RPC's).