Way to make Engine#removeAllEntities() "atomic" operation
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 930
- Forks
- 147
- PR merge metrics
- No merged PRs in 30d
Description
Hi guys, here is today problem...
Overall situation:
In some moment of my game life-cycle I need to reset world. By "reset world" I mean remove all entities from engine and then recreate game objects from scratch. Simple code may looks so:
void resetWorld() {
engine.removeAllEntities();
initiWorld();
}
void initWorld() {
// Some initial entities like "hero" create here
}
void update(float delta) {
engine.update(delta);
}
And consider all entity systems must survive through every reset and handle entity add/remove events correct. So now assume we have some sophisticate system that handles two types of entities at same time (like layer entities, and drawable entities that bound to layers). And when the system had notified about layer entity has been removed from the engine, system removes all drawable entities of that layer from the engine too.
Here I have to say that I use PooledEngine and PooledEntities that I obtain only from engine. So here finally the problem (case when pt.1 occurs outside of engine update):
- Someone calls
resetWorld(); - All entities became deleted and engine notifies entity listeners about each deletion (
Engine#notifying = true); - Our render system had been notified about layer entity removed, and now it calls
Engine#removeEntity(Entity)for each drawable entity (which actually already deleted). - Inside
Engine#removeEntity(Entity)creates newEntityOperationof remove type (because engine right now hasnotifyingflag); - Then runtime reaches
initWorld()and here some new entities create from engine pool and get filled. - Then game loop executes my
update()method and here engine finally processes all delete operations from pt.3. - Here we've got ugly bugs, some random entities get lost and deleted unexpectedly :(
All that may go even bad if you call resetWorld() during engine passing update stage. But the problem is I have no ability to check it, Engine#updating is private and has no accessor. My first question is should we add getter for it (same question about Engine#notifying)?
Now my temporary solution is to call extra engine.update(0) before initWorld() so all pending EntityOperrations will be gone. Which is not so good solution, because I cannot even imaging what may happen when I call Engine.update() inside Engine.update() (engine does not check that case...). And the other similar solution is to call initWorld() using Gdx.app.postRunnable() which does pretty the same, but it's not fits my game architecture, because there are some other things inside update() that should not be called in such state.
Perhaps the best thing that may help is extra check inside Engine#removeEntity(Entity) that will just skip entities which is not active in the system right now (Engine#entities doesn't contain it)?
Conclusion
I'm trying to find a way to remove all entities during one engine update cycle, avoiding any pending EntityOperations after that.
Thanks for read that long story and I'm kindly waiting for your thoughts :)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Engine#removeAllEntities(), Engine#removeEntity(Entity), and the EntityOperation handling described in the issue. Trace how notifying, updating, pooled entities, and listener-triggered removals interact; done means a world reset removes entities in one update cycle without pending operations deleting newly created entities.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- game-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100