Sunday, March 27, 2011

Optimizing your Game Code: Using Pools

Using pools is a standard technique in game programming. If you ever played a Real-Time Strategy game (RTS) like Starcraft, Halo Wars, Age of Empires, etc.. then you have experienced an "object pool".

In Starcraft, you construct a base, manage an economy, and train military units to tear down the other opponent until either all their buildings are gone, or until they leave. Each unit you train has a designated "food usage", meaning that you may create a unit until you run out of "supply", or your food count is lower than the required food usage. To manage this, you create supply buildings (or in zerg's case, Overlords) to increase your supply. However, you may only have a maximum of 200 food. This is called an object pool.

In Starcraft, you may use a maximum of 200 food. When a unit dies, it no longer uses that food.

You can think of the object pool as an array. You have this array of 10 circles as such:

Circle[] circles = new Circle[10];

Now, say you want to use 3 spots in the array to create 3 circles, so you would type:

for(int x  = 0; x < 3; x++)
{
     circles[x] = new Circle(3.4f); // 3.4f would be the "radius" in this case
}

Now say you wanted more than three, you would simply designate which "empty circle" you want to use to contruct a new one.

circle[6] = new circle(4.2f);

But what if you want to "destroy" one of these circles? Well, when using pools, you don't actually "destroy" an object. You simply just change its properties to make it what you call empty.

circle[3].radius = 0;

Or you can create a boolean variable "IsActive" to let the program know if it is active or not.

circle[3].IsActive = false;

When you have a property like this, you can make a constantly running function that runs through all the objects (circles, in this case), check if it is active, if not, do not use (in video game design, you can tell the engine not to render the "circles" that are inactive)

void CheckCircles()
{
     for(int x = 0; x < circles.length; x++
     {
          if(circles[x].IsActive)
               circles[x].Draw(); //If the circle IS active, then the engine will draw it on the screen
     }
}

The reason why "pooling" is standard practice is because it is an efficient way to handle multiple objects. Why would you want your game to constantly create hundreds, even thousands of objects, and then just get rid of the memory? Why not just re-use those objects? If you have any questions about pooling, please post in the comments.

54 comments:

  1. I just reinstalled SC the other day.

    ReplyDelete
  2. Thanks for the tips, I've been working on a similar project for a while and your method is perfect for what I'm doing.

    ReplyDelete
  3. nice read. any knowledge to help with game making is fine by me.

    ReplyDelete
  4. now if only you could make a script that could beat all those crazy Korean players :p

    ReplyDelete
  5. Oh, starcraft. you owned my junior high school days.

    ReplyDelete
  6. Interesting, I've never understood why it was called that.

    ReplyDelete
  7. thanks ,, this is very usefull ;)

    ReplyDelete
  8. Good old StarCraft, I've not played it in a while I've been meaning to pick up StarCraft 2

    ReplyDelete
  9. very helpfull ,thanks ill be sure to get some of that in to my sc if I ever play it again :D

    ReplyDelete
  10. o great! very useful thanks for post

    ReplyDelete
  11. looks like gibberish to me lol :P the code i mean

    ReplyDelete
  12. I never knew pools worked like this. Thanks for the infos.

    ReplyDelete
  13. Hey man, good article and im real excited you got it out there for the people to see. Web servers work the same way for the same reason. Its more efficient to have a pool of threads created up front, then to create and destroy threads as web pages are requested.

    ReplyDelete
  14. Great help, nice to see developer blogs out there.

    ReplyDelete
  15. Very interesting blog, Definitely will be following!

    Cheers,

    Mike

    ReplyDelete
  16. This is interesting, will have to try this out.

    ReplyDelete
  17. That 200 cap was always a bitch

    ReplyDelete
  18. I'm planning on going to school for coding, this is useful information for seeing how to put code into action.

    ReplyDelete
  19. awesome taking advantage of the coding! thanks bro.

    supported!
    collegeanon.tk

    ReplyDelete
  20. Wow this is great!
    I'm in my first IT year so this will be great

    ReplyDelete
  21. trying to get my head around the code but interesting stuff

    ReplyDelete
  22. Great post, i am actually just getting into game programming. Can't wait for more.

    ReplyDelete
  23. Thanks for the dev info, gonna pass it on to my friends that could use it

    ReplyDelete
  24. Thanks for the great resource. If I get back into game development, I'll definitely be using your tips.

    ReplyDelete
  25. Over-kill!! for me.. hehe but i like the picture ;) Neard..

    ReplyDelete
  26. Haven't coded in ages. Would like to try some game coding but I'm afraid can't teach new tricks to old dogs

    ReplyDelete
  27. developer blogs are always interesting to watch because you find so many things you haven't known in before :)

    ReplyDelete
  28. I wish I could code, but I am to lazy to learn it ;-)

    ReplyDelete
  29. Not familiar with this kind of stuff but I like the way you explain it

    ReplyDelete
  30. Dude you gotta make a new post soon i wanna see what youre gonna post next

    ReplyDelete
  31. Wow, that seems really complicated.

    ReplyDelete
  32. Lol @ Xuian's comment. If only there was a script to beat the Korean players.

    ReplyDelete
  33. I still have XNA somewhere around here.. might install it

    ReplyDelete
  34. Wish that was standard in all rts games. Micro-managing 100s of little buildings spoils things a lot.

    ReplyDelete
  35. looks like gibberish to me lol :P the code i mean

    ReplyDelete
  36. Developer blog, looks pretty cool, I'll have to read through the rest of your posts when I have some time.

    ReplyDelete
  37. Found you on youtub! Interesting stuff :)

    ReplyDelete
  38. that was interesting as I try to learn c++

    ReplyDelete
  39. I didnt know anything about pooling before this and I love starcraft. Great post!

    ReplyDelete
  40. Programming makes me feel dumb, probably because I am.

    ReplyDelete
  41. Um. I thought this was a guide to starcraft then I realized I was wayyyy in over my head. Got zerg rushed by a bunch of terms I've never heard of.

    :)

    ReplyDelete