Skip to content

Instantly share code, notes, and snippets.

@error454
Last active December 10, 2015 20:28
Show Gist options
  • Save error454/4488655 to your computer and use it in GitHub Desktop.
Save error454/4488655 to your computer and use it in GitHub Desktop.
Example of object pool in shiva. Two tables are created, 1 to store the objects, 1 to store boolean values representing whether the object is in-use. Each object that is part of the pool stores his own table index.
--------------------------------------------------------------------------------
function droneControl.allocateDrone ( )
--------------------------------------------------------------------------------
for i = 0, this.nDronePoolSize ( ) - 1
do
--if the entry is false the pool item is free
if( table.getAt ( this.tDroneFree ( ), i ) == false)
then
--allocate the entry
table.setAt ( this.tDroneFree ( ), i, true )
--enable the drone object
local hDrone = table.getAt ( this.tDronePool ( ), i )
object.sendEvent ( hDrone, "uberDroneAI", "onEnable" )
return hDrone
end
end
return nil
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function droneControl.createShip ( )
--------------------------------------------------------------------------------
local hDrone = this.allocateDrone ( )
if(hDrone == nil) then return nil end
--do stuff with ship
return hDrone
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function droneControl.freeDrone ( hDrone )
--------------------------------------------------------------------------------
--set the drone to free
local index = object.getAIVariable ( hDrone, "uberDroneAI", "nTableIndex" )
table.setAt ( this.tDroneFree ( ), index, false )
--disable the drone object
object.sendEvent ( hDrone, "uberDroneAI", "onDisable" )
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function droneControl.onInit ( )
--------------------------------------------------------------------------------
--initialize drone pool
local hScene = application.getCurrentUserScene ( )
--For each table, if the table is empty then initialize new objects.
--If the table is not empty, disable all objects
--Drones
if(table.getSize ( this.tDronePool ( ) ) == 0)
then
for i = 0, this.nDronePoolSize ( ) - 1
do
local hDrone = scene.createRuntimeObject ( hScene, "Swarm_Ship_4_verts" )
if(hDrone)
then
table.add ( this.tDronePool ( ), hDrone )
table.add ( this.tDroneFree ( ), false )
object.setAIVariable ( hDrone, "uberDroneAI", "nTableIndex", i )
object.setVisible ( hDrone, false )
end
end
else
--just set all entries to un-used
for i = 0, this.nDronePoolSize ( ) - 1
do
table.setAt( this.tDroneFree ( ), i, false )
end
end
scene.sendEventToAllObjects ( hScene, "uberDroneAI", "onDisable" )
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment