How To Do Multiple Runtimes?

How do you do runtime functions properly?

FOR EXAMPLE:

-I have an table filled with 200 zombies

-they have to find the closest of 200 soldiers

-then they have to move towards the closest soldier

THE QUESTION:

Is it better to have a single runtime function that loops through each of the 200 zombies every frame? Or should each of the zombies have their own runtime functions?

Thanks in advance :slight_smile:

Might not be what you had in mind, but some mixed comments:

Overall I don’t see a big difference if you have one big “update all zombies”-functions or “update this zombie”-function called for each zombie. The big question is the overall performance with 40000 distance calculations on brute force. You probably have to do something to make that 40000 at least 10000 or 4000.

Some ideas to consider:

  • Evaluate data structures like quadtrees. You can probably find a free library to use. If you can quickly get “10 nearby” and just check those it’s a huge savings
  • You should do something to quickly handle special cases like “there is no-one nearby”. Most likely in the beginning most of the zombies are not close enough to see anyone?
  • If a zombie finds a target this turn, it probably keeps the target next turn also even if it would not be exactly closest. Only if you lose the target you find a new one. It can cut overall effort (and sounds like something a zombie would do). 

Your code to test one zombie is the same regardless of method.  It comes down to overhead on processing each of those.  I would think at the machine code level, iterating over a table would be fewer instructions that the code to generate a bunch if interrupts and event handling.  The code to invoke the event handler function (popping and pushing parameters and return values, jump to the function) is more instructions than the for loop.

Whatever method, your handling needs to be as efficient as possible.

Might not be what you had in mind, but some mixed comments:

Overall I don’t see a big difference if you have one big “update all zombies”-functions or “update this zombie”-function called for each zombie. The big question is the overall performance with 40000 distance calculations on brute force. You probably have to do something to make that 40000 at least 10000 or 4000.

Some ideas to consider:

  • Evaluate data structures like quadtrees. You can probably find a free library to use. If you can quickly get “10 nearby” and just check those it’s a huge savings
  • You should do something to quickly handle special cases like “there is no-one nearby”. Most likely in the beginning most of the zombies are not close enough to see anyone?
  • If a zombie finds a target this turn, it probably keeps the target next turn also even if it would not be exactly closest. Only if you lose the target you find a new one. It can cut overall effort (and sounds like something a zombie would do). 

Your code to test one zombie is the same regardless of method.  It comes down to overhead on processing each of those.  I would think at the machine code level, iterating over a table would be fewer instructions that the code to generate a bunch if interrupts and event handling.  The code to invoke the event handler function (popping and pushing parameters and return values, jump to the function) is more instructions than the for loop.

Whatever method, your handling needs to be as efficient as possible.