entt icon indicating copy to clipboard operation
entt copied to clipboard

View on all entities except some with specific component

Open christopheseyler opened this issue 3 years ago • 7 comments

Hi, It there a way to grap all entities that do not have some component like:

registry.view(exclude<someComponent>) ?

christopheseyler avatar Aug 30 '22 11:08 christopheseyler

At the moment, you can only iterate all entities (registry::each) and if them explicitly. However, what you're asking for is on the roadmap. This is the final interface fyi:

auto view = registry.view<void>(entt::exclude<T, U>);

skypjack avatar Aug 30 '22 14:08 skypjack

Great to know. From a performance perspective, is it more efficient to create a BaseComponent to all entity and query it with my exclude, or the each + if on any_of ?

christopheseyler avatar Aug 30 '22 15:08 christopheseyler

If you create a base component and exlude it, it doesn't mean that all derived classes are also excluded as a consequence. I guess this answers your question.

skypjack avatar Aug 31 '22 07:08 skypjack

I meaned a struct BaseComponent that ensure that every entities have one (each time i create an entiti, I add this BaseComponent to it)

then, I can:

auto view = Registry.view<BaseComponent>(exclude<ComponentToExclue>); 

I would use this BaseComponent as a workaround to get a view to all my entities

christopheseyler avatar Aug 31 '22 07:08 christopheseyler

Oh, sorry, didn't get it. Then I would go with the BaseComponent since it will be easier to update later on (just replace BaseComponent with void and you're done).

skypjack avatar Aug 31 '22 07:08 skypjack

Great. BTW, that lead to a feature request : is there any way to get a signal on a new entity creation (and destruction) as we have with components instance:

It would be useful for my case of course, but I also use the hierarchy pattern you suggested. So during an entity destruction, i would like to ensure that the destroyed entity is get rid off any hierarchy

christopheseyler avatar Aug 31 '22 07:08 christopheseyler

The answer is exactly the same: the plan is to put all entities in their own storage. Then on_construct<void> is the answer.

skypjack avatar Aug 31 '22 07:08 skypjack

Available upstream on the wip branch. The way it works is:

for(auto entt: registry.view<entt::entity>(entt::exclude<T>)) { ... }

Signals on entity construction/deletion are also available as in:

registry.on_construct<entt::entity>().connect<&my_func>();

skypjack avatar Feb 22 '23 17:02 skypjack

Available on master. :+1:

skypjack avatar Mar 26 '23 12:03 skypjack

I've just noticed that the size() of this kind of view is not available:

auto myview = registry.viewentt::entity();

myview.size() returns the total number of entities

auto myview = registry.viewentt::entity(entt::exclude<MyComponent>);

myview.size() -> ERROR : size() not a member of ...

Is there any workaround ?

christopheseyler avatar Aug 05 '23 09:08 christopheseyler

size_hint @christopheseyler it tells you the maximum number of elements returned by the view. It's not guaranteed to return exactly N elements but it never returns more than them.

skypjack avatar Aug 07 '23 07:08 skypjack