BeanCheeseBurrito/Flecs.NET

FromJson and ToJson don't seem to work correctly

Thomas-X opened this issue · 3 comments

Hi, first and foremost love the work you're doing for the library!

It seems that FromJson / ToJson don't work so well, as the query from the loaded world never gets executed. The query from the world being saved is executed as it should. Feels like some issues with serialization, but not sure if it's the bindings.

Here's a quick copy-pastable console app with Flecs.NET.Debug

// Create world
using var world = World.Create();

// Create entities in world
for (var index = 0; index < 100; index++)
{
    var country = world.Entity();
    var province = world.Entity();
    country.Add<Country>();
    province.Add<Province>();
    country.Set(new Country()
    {
        Name = ("Hello world from saved and loaded world" + index).ToCharArray()
    });
    province.Set(new Province()
    {
        Name = index
    });
    province.ChildOf(country);
}

// Query entities on saved & loaded world
var query2 = world.QueryBuilder()
    .With<Province>()
    .Build();

query2.Each((Iter it,
             int i,
             ref Province c) =>
{
    var parent = it.Entity(i).Parent();
    Console.WriteLine(parent.Get<Country>().Name); // triggered
});

// Serialization

using var newWorld = World.Create();
var a = newWorld.FromJson(world.ToJson()); // a = true


// Query entities on saved & loaded world
var query = newWorld.QueryBuilder()
    .With<Province>()
    .Build();

query.Each((Iter it,
            int i,
            ref Province c) =>
{
    var parent = it.Entity(i).Parent();
    Console.WriteLine(parent.Get<Country>().Name); // never triggered
});

Note: just switched over to 4.0.1-dev-2024-04-30-06-41-46 on gitlab (was using 3.2.11 on nuget) and that seems to have the issue resolved. Not sure what the resolution of this can be

Hmm. Seems that it does correctly serialize components but not relationships. See below
image

Json serialization and deserialization is not possible with worlds that have managed types yet. I'm currently waiting on a few features to be implemented in flecs to be able to support them properly.