Removing an entity more than once - not sure if bug or user error...

57 views
Skip to first unread message

David Wagner

unread,
May 14, 2013, 5:54:21 PM5/14/13
to ash-fr...@googlegroups.com
...well, that is, it's definitely user error, but should Ash care about it? I'm not sure.

At the moment, when you remove an entity, the internal links to other entities in the list cannot be reset because that would break list iteration. However, this does leave some dangling pointers which can cause problems if the entity is removed again by error before it's re-added to the system. I've stuck together a gist with some example code and the trace output:


The second time b is removed it resurrects c. Although it's not evident from the trace output, internally the list is in a bit dodgy state - c points to d, but d points to a.

So my question is should Ash try to protect a user from abusing the API in such a way?

If the answer is yes, I can chuck a pull request your way which will solve it. For what I'm doing just now it was simpler to fix it at the engine level than to sort out my Systems mess :)

Richard

unread,
May 22, 2013, 1:43:07 PM5/22/13
to ash-fr...@googlegroups.com
Hi David

I've been wary of doing anything to fix this since anything that impacts the performance of list iteration will significantly impact the performance of Ash. On the other hand, I'd be very interested to see your solution and what the performance impact may be.

Richard

David Wagner

unread,
Jun 17, 2013, 9:54:29 AM6/17/13
to ash-fr...@googlegroups.com
Sorry for the lack of reply - my band-aid in the code worked so I forgot about this post. I've actually had a few iterations of the fix and come to the conclusion that they're all quite ugly hacks :)

At the moment I have:
  • A weak Dictionary in EntityList, which is consulted on add or remove. If an entity is in the trash dictionary, the previous and next pointers are cleared before continuing.
  • In Engine, after updateComplete is dispatched, the trashed entities in EntityList have their previous/next links cleared and the trash is reset.
This solves the problems I had but it's not infallible which is both annoying and a good indication that it's a hack. It's also not terribly elegant.

I'm going to have another think about it after I create another game/thing just to see if I can tease out the way I use it a little more. I may settle on knowing that it's an issue if entities are used the way I'm using them and note that I should create and destroy them more deterministically in the game logic.
Reply all
Reply to author
Forward
0 new messages