Class WorldSerializer
Namespace: KenseiECS
Saves a world to a stream and restores it into an empty world.
Entities keep their index and generation, so Entity values stored inside components stay valid after a round trip. The slots that hold no entity are saved too, with their generations and in the order the saved world would reuse them, so a handle that was dead when the world was saved stays dead, and the entities created after Load get the slots and handles they would have got in the saved world. Loaded into a world with a past of its own (a rollback: Clear, then Load), a free slot keeps the later of the two generations, so a handle of an entity that died after the snapshot was taken stays dead too. Component types are identified by assembly-qualified name, not by their runtime index, so the file does not depend on first-touch order.
Unmanaged components are written bit-for-bit. Components with reference fields need a registered IComponentFormatter<T>. Register<T>() (with or without a formatter) also lets Load create the pool without reflection, which matters under IL2CPP when the type is not otherwise touched.
Load fires no world events; filters and groups are populated normally. It refuses a world whose pools still hold components without an entity (left by a DestroyEntity that a listener or AutoReset interrupted): call world.Clear() first. Pool and filter listeners do run, as for any Add. If one destroys an entity that is being restored, the components of that entity not restored yet are skipped (they go neither to the freed slot nor to an entity the listener created in it). A component that a listener has already added to a restored entity when its stored value arrives is overwritten with that value, without a second OnAdded.
Snapshots written by 2.4.0 and earlier (format version 1) still load; they hold no generations of free slots, so in a world restored from one a handle that was dead at Save can name the next entity created in its slot.
Usage:
var serializer = new WorldSerializer();
serializer.Register(new InventoryFormatter());
using (var file = File.Create(path)) { serializer.Save(world, file); }
world.Clear();
using (var file = File.OpenRead(path)) { serializer.Load(world, file); }public sealed class WorldSerializerInheritance
Inherited Members
object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()
Methods
Load(World, Stream)
Restore a snapshot into an empty world. Throws if the world has entities or the stream is not a snapshot. Called by a listener (a rollback inside a callback), it ends what is still running about the entities that are gone, as Clear() does: the restored entities, whose handles may be the same, get no event of it.
public void Load(World world, Stream stream)Parameters
world World
stream Stream
Register<T>(IComponentFormatter<T>)
Register a custom formatter for T.
public void Register<T>(IComponentFormatter<T> formatter) where T : struct, IComponentParameters
formatter IComponentFormatter<T>
Type Parameters
T
Register<T>()
Register an unmanaged component type so Load creates its pool with a direct call instead of reflection.
public void Register<T>() where T : struct, IComponentType Parameters
T
Save(World, Stream)
Write all alive entities and their components. The stream stays open. Called by a listener of a DestroyEntity, it saves the world without the entity being destroyed; components that an interrupted DestroyEntity left without an entity are not saved.
public void Save(World world, Stream stream)Parameters
world World
stream Stream
