Object Pooling in CoronaSDK

Creating and removing game objects is a very expensive operation. Couple that with enabling and removing physics properties on these objects during game play, and it is no wonder you will notice your game stuttering and lagging during these operations. As more objects are added to the screen, the situation gets worse.

The concept of Object Pooling is something that every new game developer needs to understand. With Object Pooling, you create a set of game objects and configure them as much as you can before the game starts, usually during scene load. During game play, you use and reuse these previously created objects.

Fortunately for Corona developers, creating an object pool is very easy. In this article, we’ll step through this to create a pool of reusable game objects. As an example, I will use one sprite from my game Space Mission: Survival, and I will show you how I create a pool of enemy ships.

Some things to note… for simplicity, I am going to create a pool of static images. In my games, I create my pools using sprite sheets, so I can animate these game objects, but that is outside the scope of this tutorial.

Step 0… Planning

You should determine how you want to pool your game objects. Personally, I like to pool my objects by type, and not mix them.  For example, Space Mission Survival contains an enemy pool and a bullet pool. By keeping them separate, you can manage them easier.  Further, my game contains a bullet pool for the player and a bullet pool that is shared by all enemies, even though the bullet sprite is identical.

Also note that since you are creating a pool of objects, memory will be allocated so try to determine the amount of objects you really need.  For example, if you know you need 50 enemies, create 50 objects, not one hundred.  The extra ones won’t be used, and they will consume memory. In cases where you are not really sure how many you need, you can create extra objects during times where there is little action going on (level starts, info screens, etc).

Step 1… Creation

So, what’s a pool in Corona?  It’s just a table of sprites. Let’s create a pool of enemies:

local maxEnemies = 50
local Enemies = {}

for i = 1, maxEnemies do
    Enemies[i] = display.newImage("enemy.png", 0, 0)
    Enemies[i].isVisible = false
    Enemies[i].isAlive = false

    -- if you need physics on these game objects
    physics.addBody(Enemies[i], "dynamic", params)
    Enemies[i].isBodyActive = false
end

The for loop creates and adds 50 enemies to a table named Enemies, and sets them invisible. Line 7 is a property I added which will keep track of live enemies (enemies currently on the screen).

If you need physics on these game objects, lines 10 and 11 set that up. Line 11 disables any physics checks on this object since it is not alive.

That’s it! This code takes care of the the processor intensive creation of objects that would otherwise slow your game down during game play.

You now have a table of game objects set up with physics, but they are not in the game.  How do you use them?

Step 2 – Use
-- return an available enemy, or nil if there is no enemies left
local function spawnEnemy()
    for i = 1, #Enemies do
        if not Enemies[i].isAlive then
            return Enemies[i]
        end
    end

    -- if we get here, there are no more available enemies in the pool
    return nil
end

-- upon death of enemy, back to the pool
local function killEnemy(enemy)
   enemy.isBodyActive = false
   enemy.isVisible = false
   enemy.isAlive = false
end

-- get enemy from the pool and activate it
local enemy = spawnEnemy()

if enemy ~= nil then
    enemy.x = 100
    enemy.y = 100
    enemy.isVisible = true
    enemy.isAlive = true
    enemy.isBodyActive = true
end

-- do a bunch of stuff in your game and kill enemy when appropriate
killEnemy(enemy)

In the above code, the spawnEnemy function will return a reference to an available enemy in the enemy pool, or nil if one is not available.  The killEnemy function will return the enemy back to the pool.

Step 3 – Clean Up

When done, don’t forget to clean up! Cycle through the pool to remove all event listeners and physics, and don’t forget to nil out the game objects.

Conclusion

Utilizing object pooling is a great way to keep your game responsive and allows you to mange objects by creating and using only what you need. Object reuse keeps your game from having to allocate and release memory by creating the same objects over and over.

This was a basic tutorial on how to get started with object pooling in CoronaSDK. Play around a bit… use sprite sheets to animate pooled objects, or change the sprite of an object. Create a dynamically growing pool in cases where you need more objects than you created. Or, create a generic pooler library to handle creation, deletion, spawning, and removal of all objects in your game.

4 thoughts on “Object Pooling in CoronaSDK”

  1. hii…its really nice to have such kind of post which makes programming related to game development much easier…but i have some doubt ,,may i am wrong but i want to ask….my problem is step 2 line 21. here we declare an variable(enemy) and assign a value by calling a function i.e(spawnEnemy).. so my doubt is when we caal this function ,is it supposed to complete a for loop and each time return a value to variable enemy.?so we have a single variable enemy and every single time we call function i.e enemyspawn for loop gets initiated
    and we have so many values for variable enemy. so is it overwriting a single variable by different values or i am missing something which is really happening here.

    and i want to apologize,,because this is not a right place for queries

  2. No apologies needed… thanks for your comment.

    Line 21 in step 2 will grab an unused object from the pool and store it in enemy. If you need multiple enemies, then you either need additional variables, or better yet, store them in a separate table.

    This example of pooling is a very simple approach. When you request a new object, it does loop through an internal table of enemies looking for an unused one, and returns it, if one exists. the goal of this this blog entry was to help people new to object pooling understand the concept and using this approach was the easiest way to do it.

    I am currently working on a much better (optimized) approach, and I hope to have something announced shortly.

Leave a Reply to Mario Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: