Skip to content

Duplication of unnamed entities with recycled ids when using world.from_json() #1498

@Terzalo

Description

@Terzalo

When an entity does not have a name assigned and is reusing an id of a previously destroyed entity, serializing and deserializng it with world.to_json() creates a copy of it with a different id instead of overwriting the existing entity.

Example code:

#include "flecs.h"
#include <cstdio>

int main()
{
    flecs::world world;

    auto e1 = world.entity().add(flecs::Prefab);
    printf("e1 id: %u\n", e1.id());
        // e1 id: 540
    e1.destruct();

    auto e2 = world.entity().add(flecs::Prefab);
    printf("e2 id: %u\n", e2.id());
        // e2 id: 540

    auto json = world.to_json();

    printf("world.to_json(): %s\n", json.c_str());
        // world.to_json(): {"results":[{"name":"#540", "id":540, "tags":["flecs.core.Prefab"]}]}

    world.from_json(json);

    json = world.to_json();
    printf("world.to_json(): %s\n", json.c_str());
        // world.to_json(): {"results":[{"name":"#540", "id":540, "tags":["flecs.core.Prefab"]}, {"name":"#541", "id":541, "tags":["flecs.core.Prefab"]}]}

    world.from_json(json);

    json = world.to_json();
    printf("world.to_json(): %s\n", json.c_str());
        // world.to_json(): {"results":[{"name":"#540", "id":540, "tags":["flecs.core.Prefab"]}, {"name":"#541", "id":541, "tags":["flecs.core.Prefab"]}, {"name":"#542", "id":542, "tags":["flecs.core.Prefab"]}]}

    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions