hmans / miniplex

A 👩‍💻 developer-friendly entity management system for 🕹 games and similarly demanding applications, based on 🛠 ECS architecture.
MIT License
829 stars 38 forks source link

"Changed" iterable in archetype? #154

Open mikecann opened 2 years ago

mikecann commented 2 years ago

Firstly really nice work with this library!

I am in the process of looking for something new for our game battletabs.com and have been working my way through all the options listed here: https://github.com/noctjs/ecs-benchmark. Although I would love to get the best performance I can I just don't like the developer experience sacrifices that you have to make to make it as performant as an SoA implementation and quite frankly I dont think our game needs that level of ultra-performance.

One thing I was wondering about however was that bitecs has a concept of a "changed" query (https://github.com/NateTheGreatt/bitECS/blob/master/docs/INTRO.md#-query). This means that you handle entity additions and removals from an archetype within your main system loop rather than having some systems activated out-of-band.

What are your thoughts on this?

hmans commented 2 years ago

Hi again! Thanks for the kind words, I am happy that you are enjoying the library.

I need to think about this a little. From my gut, I would say that you could model this with the current tools by hooking into an archetype's onEntityAdded and onEntityRemoved events, and letting the callback create "event" entities that represent this information, and will be processed by a separate system. Kinda like that (blind code):

const withHealth = world.archetype("health")
withHealth.onEntityAdded.add((e) => world.createEntity({ entityAdded: e }))
withHealth.onEntityRemoved.add((e) => world.createEntity({ entityRemoved: e }))
/* etc. */

Would that help in your situation?

(Addendum: these events are currently internal and thus not documented, and might change in signature or implementation, so I wouldn't currently recommend hooking into them, but I'd be happy to promote them to an official feature.)

mikecann commented 2 years ago

Thanks for the reply.

Ye I think it would be great if you made them public they are useful for many things such as hooking into existing libraries and runtimes.

Ye I thought it would be possible to do the archetype in userland but I was wondering if you had any thoughts on best-practices, should you handle entity additions inline-rather than out-of-band so that everything is deterministic and system-order is preserved.

See this example from BitECS for how its managed there: https://github.com/ourcade/phaser3-bitecs-getting-started/blob/master/src/systems/sprite.ts#L22

hmans commented 1 year ago

I accidentally stumbled over a great pattern while working on 2.0 that I think will solve this nicely. Stay tuned!