Dynamic Loop Handler (CMS)

From The Official Visionaire Studio: Adventure Game Engine Wiki
Revision as of 15:22, 14 August 2014 by AFRLme (talk) (Created page with "''To do... - it works though!'' ;) Working example... <syntaxhighlight> --[[ Dynamic Loop Handler [v2] (01/07/2014) Written by AFRLme [Lee Clarke] -- + -- alternatingfrequen...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

To do... - it works though! ;)


Working example...

--[[
Dynamic Loop Handler [v2] (01/07/2014)
Written by AFRLme [Lee Clarke]
-- + --
alternatingfrequencies@hotmail.com | skype @ AFRLme
-- + --
This script is donation optional. In game credit is non-negotiable.
You are free to: ¹ use it in your game(s). ² modify the script.
Do not remove - or edit - this comment block.
--]]

-- * tables * --
local t = {} -- create a new local table
t["_temporary_"] = "" -- set table as temporary

-- * function for creating a new loop * --
function addLoop(n, d, a)
 if table.maxn(t) > 0 and not checkLoopExists(n) or table.maxn(t) <= 0 then -- check if loop table is less than or more than 0 & if loop already exists
  table.insert(t, n); t[n] = {delay = d, amt = a, time = getTime()} -- create table & insert delay & amount values
  registerEventHandler("mainLoop", n) -- start the loop
 end
end

-- * function for checking if loop already exists * --
function checkLoopExists(n)
 for i = 1, #(t) do if t[i] == n then return true elseif i == table.maxn(t) and t[i] ~= n then return false end end -- if loop exists return true else return false
end

-- * function used to kill the loop * --
function killLoop(n)
 for i = 1, #(t) do if t[i] == n then table.remove(t, i); break end end -- find the loop index value
 unregisterEventHandler("mainLoop", n) -- remove the loop data from the table
end

-- * function which determines if loop should be executed * --
function getLoopStatus(n)
 if getTime() >= t[n].time + t[n].delay then -- if current time is equal to or greater than previous time + delay
  if t[n].amt > 0 or t[n].amt == nil then -- if loop amount if greater than 0 or equal nil
   if t[n].amt ~= nil then t[n].amt = t[n].amt - 1 end -- if loop amount does not equal nil then loop amount - loop amount - 1
   t[n].time = getTime() -- update loop start time
   return true -- execute loop
  else killLoop(n); return false end -- end if 1
 else return false end -- end if 2
end -- end function

function loop001()
 if getLoopStatus("loop001") then
  print("001: " .. t.loop001.amt)
 end
end

function loop002()
 if getLoopStatus("loop002")  then
  print("002: " .. t.loop002.amt)
 end
end