The latest update to GBC Object Pool now supports the passing of parameters to your create and return functions! By passing in a table of key/value pairs, you can reduce the amount of create and return functions needed when creating object pools.
Previously, you had to code a separate create function for every pool you created. Seems like a lot of code for creating similar objects.
Check out this example below:
Old Way
local function CreateImagePool1()
local obj = display.newImageRect("image.png", 50, 50)
physics.addObject(obj, "dynamic", {
friction = myParams.friction,
bounce = myParams.bounce,
})
obj.id = myParams.id
return obj
end
local function CreateImagePool2()
local obj = display.newImageRect("image.png", 50, 50)
physics.addObject(obj, "dynamic", {
friction = myParams.friction,
bounce = myParams.bounce,
})
obj.id = myParams.id
return obj
end
imagePool_1 = GBCObjectPool.init(CreateImagePool1)
GBCObjectPool.create(imagePool_1 , 10, 0, false, {
friction = 0.1,
bounce = 0.1,
id = "Image Pool 1"
})
imagePool_2 = GBCObjectPool.init(CreateImagePool2)
GBCObjectPool.create(imagePool_2 , 10, 0, false, {
friction = 0.25,
bounce = 0.5,
id = "Image Pool 2"
})
New Way
local function CommonCreateFunction(myParams)
local obj = display.newImageRect("image.png", 50, 50)
if myParams ~= nil then
physics.addObject(obj, "dynamic", {
friction = myParams.friction,
bounce = myParams.bounce,
})
obj.id = myParams.id
end
return obj
end
imagePool_1 = GBCObjectPool.init(CommonCreateFunction)
GBCObjectPool.create(imagePool_1 , 10, 0, false, {
friction = 0.1,
bounce = 0.1,
id = "Image Pool 1"
})
imagePool_2 = GBCObjectPool.init(CommonCreateFunction)
GBCObjectPool.create(imagePool_2 , 10, 0, false, {
friction = 0.5,
bounce = 0.25,
id = "Image Pool 2"
})
In the example above, using the old method, you had to code a new create function for every pool you wanted to use. This was a lot of code, especially for pooled objects that had very similar, but different, settings. Notice that friction and bounce settings are slightly different. Same with your return to pool function. It gets worse as you add more pools.
Using the new method, we use a single create function to create multiple image pools, and pass in parameters that are used to modify physics properties for the pool. We also add an id field to each image to show that you can create your own fields to store data in each object.
Another example demonstrates passing in the name of the actual image into the create function:
local function CommonCreateFunction(myParams)
if myParams ~= nil then
local obj = display.newImageRect(myParams.image, 50, 50)
physics.addObject(obj, "dynamic", {
friction = myParams.friction,
bounce = myParams.bounce,
})
obj.id = myParams.id
end
return obj
end
imagePool_1 = GBCObjectPool.init(CommonCreateFunction)
GBCObjectPool.create(imagePool_1 , 10, 0, false, {
image = "coolimage.png",
friction = 0.1,
bounce = 0.1,
id = "Image Pool 1"
})
imagePool_2 = GBCObjectPool.init(CommonCreateFunction)
GBCObjectPool.create(imagePool_2 , 10, 0, false, {
image = "anothercoolimage.png",
friction = 0.5,
bounce = 0.25,
id = "Image Pool 2"
})
In the example above, we use a common create function to create an image with physics properties. Passing in the parameters for the actual image to use allows us to have a single, generic create function instead of creating multiple functions to create your pool.
Examples Available
Check out the GBC Object Pool Git for source code and compiled examples. If you have any questions, I am available in the Corona Forums.
[divider]