BeanCheeseBurrito / Flecs.NET

A C# wrapper for flecs
MIT License
145 stars 18 forks source link

FromJson and ToJson don't seem to work correctly #24

Closed Thomas-X closed 6 months ago

Thomas-X commented 6 months ago

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
});
Thomas-X commented 6 months ago

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

Thomas-X commented 6 months ago

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

BeanCheeseBurrito commented 6 months ago

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.