SanderMertens / flecs

A fast entity component system (ECS) for C & C++

Home Page:https://www.flecs.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Best way to share a component between differents entities

Krixtalx opened this issue · comments

Hi!

I'm using flecs as part of my renderer and i want to know if sharing a single component between differents entities is possible. I'm planning to use it for sharing model data between differents entities that shares the same 3d model. A more generic use case: imagine an RPG, with predefined weapons. Instead of each entity having a copy of x weapon, they refer to the one already instanciated. Something similar to shared_ptr.

I have thought about the relationships system, but I see the examples a bit faraway of my use case and i don't really know if that is what i'm looking for. I have seen the flecs::ref, but as far as I can tell, that isn't solving my problem.

Feel free to change the title to something more appropiated if needed.

EDIT: For anybody with the same problem, there is a section in the quickstart about instancing, exactly this problem. It's basically what copygirl said in the comment below, but it seems implemented in a way to be used like this.

You could add a (IsA, $sword_model) relationship to each sword entity, where $sword_model is an entity that has the component(s) necessary to define the model. Though I don't know the exact mechanics and if querying will work as expected.

Personally for an abandoned project I played around with creating a Model component that holds the model data, created entities for each model and then similarly created a (Model, $model_entity) relationship for my entities. Looking up the model this way requires using a Rule or running an additional query.

Yeah, that was what i thought, but I see it as an over complicated solution for something that should be more straight forward.

Closing this as i found out the discord and that is a more appropiate place to put questions.