amethyst/legion

Allow serializing/deserializing Entity outside World

Rua opened this issue · 4 comments

Rua commented

When trying to serialise a type that contains an Entity reference, outside a World, I get a panic with the error "No entity serializer set". Serializing an Entity reference requires run_as_context to be called in order to provide the thread-local variable containing the Canon. This function is private, which means it's not possible to serialize/deserialize any type that contains Entity if it's not in a component in the World.

Could something be added to allow serde of Entity outside a World?

I think #221 solves this but I'm not sure what idiomatic code looks like using it. (note that this PR isn't part of the last released version)

Rua commented

Unfortunately it doesn't. The way to serialise Entity or types containing it would be something like this:

registry.with_entity_serializer(|canon| {
    run_as_context(canon, || {
        // do serialising here...
    });
});

This is more or less what the code here does. But with run_as_context being private, non-Legion code can't replicate it.

I am seeing this as well

I'm storing copies of Entity in a scene graph outside of the legion::World. When the scene graph is empty (containing no copies of Entity) serialization works properly, but otherwise I get the No entity serializer set panic when serializing

Well, in the meantime, I have a workaround for this.

Just before serializing, put structs containing entities in the world as single-component entities and serialize as normal. Then remove them from the world.

When deserializing extract theses structs and remove them from the world. Note that this workaround does a lot of copying :/