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

Allow managed types with .Ctx() for systems/observers, and update AppBuilder.Init() callback signature #76

Merged
merged 1 commit into from
Nov 22, 2024

Conversation

BeanCheeseBurrito
Copy link
Owner

This PR allows managed types to be passed as user context to systems and observers. Previously only pointers to unmanaged types were allowed.
Old:

int value = 10;

world.System()
    .Ctx(&value) // Pointer has to be passed.
    .Each(static (Iter it, int _) =>
    {
        ref int ctx = ref it.Ctx<int>();
        Console.WriteLine(ctx); // Prints 10
    });

New:

world.System()
    .Ctx("Context") // Context is passed by value
    .Each(static (Iter it, int _) =>
    {
        ref string ctx = ref it.Ctx<string>();
        Console.WriteLine(ctx); // Prints "String"
    });

A callback can be provided to run clean-up logic before the context object is freed.

world.System()
    .Ctx("Context", static (ref string ctx) =>
    {
        Console.WriteLine("Context is being freed");
    }) 
    .Each(...);

The callback signature for AppBuilder.Init() has been changed to accept a World instead of ecs_world_t*

Old:

using World world = World.Create();

world.App()
    .Init(Setup)
    .Run();

static void Setup(ecs_world_t* worldPtr)
{
    World world = World.Create(worldPtr);
    world.Import<Module1>();
    world.Import<Module2>();
    world.Import<Module3>();
}

New:

using World world = World.Create();

world.App()
    .Init(Setup)
    .Run();

static void Setup(World world)
{
    world.Import<Module1>();
    world.Import<Module2>();
    world.Import<Module3>();
}

@BeanCheeseBurrito BeanCheeseBurrito merged commit ab5d078 into main Nov 22, 2024
6 checks passed
@BeanCheeseBurrito BeanCheeseBurrito deleted the managed-user-contexts branch November 22, 2024 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant