Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

How does Flecs.NET handle passing C# defined types to C++? #38

LouChiSoft started this conversation in General
Discussion options

Hi, I am trying to make some kind of addon/extension for Godot that enables the use of Flecs. There is an example of it here: https://github.com/paulfigiel/godot-flecs-sample, but it's limited in scope and functionality. It only really works if you intend to implement all your components/systems/etc. in C++ and just manage the calling of them from the editor.

I want to make something more flexible that has proper support for GDScript including defining new Components and Systems but I have ran into a bit of an issue. Basically because of the way that GDScript for Godot works, when you extend an object (e.g. MyComponent_1 extends Component) you don't actually get a new type in C++. This means that if I wanted to add MyComponent_1 and MyComponent_2 to an entity, it would actually overwrite the first component since from a C++ perspective they are both still a Component object.

So this got me thinking, does Flecs.NET have the same issue? It must do something so that the C++ code can recognise the different types defined in C#. I tried having a look into your source code, but I am not amazing at reading other people's source code.

Any help would be appreciated

You must be logged in to vote

Replies: 2 comments · 1 reply

Comment options

The flecs core is type erased so it is up to the language binding to distinguish between different component types. In Flecs.NET, the first time a component is used, it does a symbol lookup to see if an existing entity exists and will bind to the existing entity if the size and alignment match.

For example, if you registered a component named "Position" in C++ with an id of 100, and later tried to register a "Position" component in C#, the C# wrapper will use the id of 100 assuming both components have the same size and alignment. This id gets cached and reused for the duration of the application.

I don't know if what I do in C# is possible in GDScript. A Godot binding for flecs is being worked on here that might give you some ideas on how to approach registering GDScript types in flecs. https://github.com/GsLogiMaker/glecs_godot_plugin/tree/master

You must be logged in to vote
0 replies
Comment options

Can C# managed objects be passed to flecs? That is a component with a C# string or list. If it is possable what structures would be required.

I was thinking a dict of arc wrapped objects managed by component lifetime events outside of flecs. Systems that need to used objects in will reference the arc. This would avoid the need for pinning and other tricks but increase overhead.

You must be logged in to vote
1 reply
@BeanCheeseBurrito
Comment options

Flecs.NET supports managed types as components. There is no special setup required by the user, you can add strings or lists to components like you normally would with unmanaged types.

Entity e = world.Entity().Set("Hello World.");

world.System<string>().Each(static (ref string str) =>
{
    Console.WriteLine(str); // Prints "Hello World."
});

The code below is a rough example of what Flecs.NET does internally. We allocate a GCHandle for the managed object and pass it to flecs.

// Setting component
StrongBox<T> box = new StrongBox<T>("Hello World."); //  Store string in strong box
GCHandle handle = GCHandle.Alloc(box) // Allocate GCHandle for strong box
ecs_set_id(world, entity, component, sizeof(GCHandle), &handle); // Pass GCHandle to flecs

// Getting component
GCHandle* handle = (GCHandle*)ecs_get_id(world, entity, component); // Retrieve GCHandle
StrongBox<string> box = (StrongBox<T>)handle->Target! // Grab strong box
ref string str = ref box.Value!; // Return reference to string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.