Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

9 years ago by jminor

If you call this.kill() from inside an entity's update() function, then the next entity in the scene will not be updated that frame. This is because game.update() iterates over the list of entities by index, but kill() removes one from the list.

There is a similar problem where calling ig.game.loadLevel() inside an update function will cause odd behavior since the list of entities has been completely swapped out in the middle of the update loop. For example, entities early in the new level's list will be drawn before their first update is called.

Here are a few ways to fix these bugs: 1. Make a copy of the entities list and iterate over that. 2. Iterate over the list backwards. 3. Defer the removal of entities and level switch until after the loop is done.

9 years ago by dominic

Mhh... thinking about it, it's even more complex than that. Entities can not only kill themself's in their own update function, but also can kill other entities (through receiveDamage() etc.). I'll have to make sure that these "dead" entities don't get updated, don't collide and don't get checked against other entities.

As for loadLevel() - I think ideally when it's called, the current update loop should cancel completely for this frame.

Strangely enough I never noticed these effects, but I guess they can be pretty severe. Working on a fix right now. Thanks for your detailed report :)

9 years ago by MikeL

In terms of the entity problem, I kind of like jminor's third option. One possible way is to change the entity kill() function to simply contain a boolean:

isKilled = true;

Do not remove the entity from the list. Anything that has isKilled == true will not be able to kill other entities, collide etc. Then, at the end of game.update(), iterate through the entire list again and remove entities that have isKilled == true.

9 years ago by dominic

Fixed the entity kill/update issue like you suggested. It's in the current dev version in the git repo.

I also added a .loadLevelDeferred( data ) method for ig.Game, that should be used when you want to load a level in the midst of an update() cycle.

9 years ago by jminor

Thanks! loadLevelDeferred works perfectly!

9 years ago by MikeL

Dominic, I believe that you will also need to add:

ent.type = ig.Entity.TYPE.NONE;

to removeEntity in order to prevent another entity from colliding with the _killed entity. At least that's what I found when working on a similar problem.
Page 1 of 1
« first « previous next › last »