0

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?

user2738698
  • 957
  • 1
  • 8
  • 20

1 Answers1

3

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.

Dan Pichelman
  • 13,773
  • 8
  • 42
  • 73