By rescue86k

So my game is getting bigger, and keeping on top of lag is a high priority. Saying that I run the fps at 50, I never want it to go bellow a certain amount (such as 30, for a full blown battle, or 45 if only 2-3 objects/NPCs moving around).

Considering a big game, such as your own, you found arrays to be quite helpful. In my game, I have an enemy array (enemies) that checks hitTest() for each wall in the second array (walls) using a nested loop, presented in the base Avoider Game Tutorial. I assume you have completed that series, and you know what a nested loop is so I wont go into explaining it if you don’t mind.

I’m about to add a third array (allies) which will include the avatar in it. Problem with performance is this:

if currently you have enemies with nested walls, then you have that code being checked X times like so: 

enemies[# of arguments]  *  walls[# of arguments] = amount of hitTest checks.

Follow me?

So if there are 3 enemies in the enemies array, and 5 walls in the walls array, then the total number of checks would be:

3*5= 15

thus 15 times the code went through. Ok not bad. Lets add my allies array’s loop now by nesting it in the walls array’s loop.

enemies[# of arguments]  *  walls[# of arguments] *  allies[# of arguments] = amount of hitTest() checks.

There are three allies, the first argument is the avatar, the second two are your buddies fighting with you. So

3*5*3= 15*3, then 15*3 = 45

45 checks! 3 times as much strain put on flash player! Answer is low fps,  (regarding my game). What do we do?

Well I need each enemy and ally to recognize hitTests between each other as well as walls. Try this:

enemies[# of arguments]  * allies[# of arguments] = subtotal1 amount of hitTest checks.

enemies[# of arguments]  *  walls[# of arguments] = subtotal2 amount of hitTest checks.

allies[# of arguments]  *  walls[# of arguments] = subtotal3 amount of hitTest checks.

subtotal1 + subtotal2 + subtotal3 = amount of hitTest checks.

so

3*3=9;

3*5=15;

3*5=15;

9+15+15=39;

39 sounds better than 45 :)

Why is this such a big deal? Well the answer is everything you do to your game should be a big deal if you plan on having a big game. If you could cut out every “fee” you got in life for a year, such as $2 for an ATM transaction, would it add up? Games work the same way. Down to the graphics. So now lets suppose you have:

20 walls, 2 enemies, and 1 ally (the avatar with no buddies in current level). Would the checking work the same? Save you a few checks, like 6 last time? Lets find out:

The old way (allies nested in enemies, walls nested in allies):

2*1*20=40

The new way(allies nested in enemies, walls nested in allies, walls nested in enemies)

2*1=2

2*20=40

1*20=20

2+40+20= 62 

What? The old way was better this time. I guess this goes to show that you really need to pay attention to how many loops you have, and how many checks are going through with your current setup. If anyone has a more efficient way of doing this, please let me know.  

Any questions?

2 Responses to “Performance of Nested Loops in Bigger Games”

  1. [...] In the second, Kevin starts off a discussion on the efficiency of nested loops, showing that the optimum code in one case may turn out to be a poor solution in another. It’s open-ended, so throw in your opinion here [...]

  2. MichaelJW says:

    Weird, isn’t it? Hey, you might want to check out this mathematical function to help predict how many combinations you’re going to have to run through: http://en.wikipedia.org/wiki/Combination

Leave a Reply