Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Terzalo opened this issue Jan 4, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@Terzalo
Copy link

Terzalo commented Jan 4, 2025

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;
}
@Terzalo Terzalo added the bug Something isn't working label Jan 4, 2025
@Terzalo
Copy link
Author

Terzalo commented Jan 4, 2025

A workaround is to set the name to something other than #<id>

// set the name to be "e540" instead of "#540"
e2.set_name( std::format( "e{}", uint32_t(e2.id()) ).c_str() );

@SanderMertens
Copy link
Owner

Ah, the reason for this is that the entity version isn't stored in the serialized data. Will take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants