Skip to content

Snapshot Deserialization Lifecycle #203

@ben-zabloski

Description

@ben-zabloski

Issue

When using the snapshot deserializer, onAdd handlers are called before data is present.

Description

When using the snapshot deserializer, entities are created, and components are added. The problem I have is when data is populated into the components. When I initialize systems, I have code like this:

ecs.observe(world, ecs.onAdd(DisplayGraphics), (entityId) => {
  log.debug('graphics', 'onAdd(DisplayGraphics)', entityId);

  const graphics = new Graphics();

  const shape = DisplayGraphics.shape[entityId];

  if (shape === DisplayShapeType.Rectangle) {
    const color = DisplayGraphics.colour[entityId] || 0x13678a;
    const width = (DisplayGraphics.width[entityId] || 0) * 1;
    ...

Generally, I have entity creation helpers that will populate data before components are added, but I don't think I have a way out when components are instantiated with the snapshot deserializer, at least without changing my framework's lifecycle somehow.

Workarounds

I can stop using the onAdd observe handler, and manage things through the update loop, or I can adjust my framework to have a sort of initialization/ready phase 🤔

Just wondering if there might be an alternate solution I haven't noticed 😊

bitECS

I'm enjoying bitECS! Thanks for sharing ❤️

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions