Open HyperCodec opened 6 days ago
This feels like a great use case for lifecycle hooks and observers. Do you want it to operate on literally any entity? Can you say more about what you're ultimately trying to do here?
This feels like a great use case for lifecycle hooks and observers. Do you want it to operate on literally any entity? Can you say more about what you're ultimately trying to do here?
I think the best solution is making it restricted via type and propagating values like a query. To operate on any entity, someone could put EntityId
as the type (i.e. EntitySpawnEvent<EntityId>
), but to receive the components it could be likeEntitySpawnEvent<&Transform>
or even use a tuple to get multiple. There's already code for queries so it likely wouldn't be too difficult to implement with the event.
It's probably also a good idea to have some sort of receiver checker: have a collection somewhere with the query type id/info and then add the types of a system's EventReader
s when the system is registered. Then the dispatching code could check which queries the object matches against. This would eliminate a lot of extra work and overhead from dispatching events with no receivers. Could probably be made into an API for events in general.
What problem does this solve or what need does it fill?
It would make the detection of new entities faster and easier.
What solution would you like?
Probably a builtin
EntitySpawnEvent
type that gets dispatched whenevercommands.spawn
is used. It could contain the ID of the entity or something.What alternative(s) have you considered?
There's
Added<SomeComponent>
, but that requires knowing/having access to the component and isO(n)
. Using an event system would be faster and more flexible in cases where someone would want to know if any entity spawns.Additional context
Using a generic like
EntitySpawnEvent<SomeComponent>
would be much faster for retrieving specific component data because using the entity id withQuery::get
is still pretty close toO(n)
. The components could be checked once percommands.spawn
call if the component type is restricted on the event level.