I'm making a prototype for a game. I'm working on effects at the moment, but more generally animation.

Currently, I have list where newly spawned effects are added to. Then on the update section of the game loop, I'd let each effect change their state based on time. When an effect finishes, I have it trigger an internal flag. After I update all of the effects, I check the list over again for any finished effects and remove them from the list.

It works. I figured it can be done a better way, but I'm not sure how. I mean, it runs through the list twice to update and purge finished effects. If I use an array, deletion will leave a hole there, and I'd have to track where the holes are, which seems more complicated. I figure a linked list is best? Any suggestions?

closed as primarily opinion-based by Scant Roger, user53019, user40980 Nov 24 '15 at 22:29

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.


It's hard to say, given what you've put into your question (no mention of language or performance requirements).

An array will work. As you noted, you can remove finished effects then add logic to handle the holes. If it's an array of objects, just setting that element to NULL should suffice. You can also have a field isFinished inside an object if you prefer.

Depending on your language and performance requirements, you may be able to use something like C#'s List<>. Java has a similar class. You can think of it as an array with support for deleting without holes.

There's nothing wrong with a linked list, but I'd be a little concerned about the possibility of premature optimization.

Stick with the simplest code that works, wait until you have a measurable performance problem, then use the scientific method to isolate the code that needs optimizing.

Not the answer you're looking for? Browse other questions tagged or ask your own question.