Page 1 of 1
How to call counter function
Posted: Fri Aug 21, 2015 10:08 am
by barronvonburp
I have no idea what I am doing, but I know what I want to do.
I want to call the value of a specific counter, and depending on what that value is I want to activate a function.
Re: How to call counter function
Posted: Fri Aug 21, 2015 2:59 pm
by THOM
the code to get a counters value is
a simple code would be
Code: Select all
if mycounter.counter:getValue() == 5 then
mydoor.door:open()
end
Re: How to call counter function
Posted: Fri Aug 21, 2015 5:05 pm
by Isaac
Additionally, one can use a table to increment through the actions.
Like this:
Code: Select all
actions = {
function() hudPrint("Hello World") end,
function(dir) local dir = dir or (party.facing+2)%4 party.party:knockback(dir) end,
function() actions[2](party.facing) end,
function(loc) if not loc then loc = {15,15,0,1,1} end if type(loc) == "table" then party:setPosition(unpack(loc)) end end,
function() party.party:turn(1) actions[4]({15,15,(party.facing+1)%4,0,1}) end,
function(dir) local dir = dir or 1 party.party:turn(dir) actions[4]({party.x,party.y,party.facing,party.elevation,party.level}) end,
function() actions[6](-1) end,
function() party.party:swapChampions(math.random(2), math.random(3,4)) end,
function() playSound("secret") end,
function() damageTile(party.level, party.x, party.y, party.facing, party.elevation, 64, "physical", 2) party.party:shakeCamera(1,1) playSound("party_crushed") end,
function() party.party:heal() end,
function() party.party:rest() end,
function() local imp = party:spawn("trickster") local dX,dY = getForward(imp.facing) imp:setPosition(imp.x +dX, imp.y+dY, (imp.facing+2)%4, imp.elevation+1, imp.level) end,
function() counter_1.counter:setValue(0) end,
}
function selectFromCounter()
local val = counter_1.counter:getValue()
if val>0 and val<= #actions then
actions[val]()
end
end
*Add a counter and Connect a button or plate to the script and it will cycle through a table of functions; one for each press.
_________________________
Another way to do this: (This one allows for descriptive names for your actions.)
Code: Select all
function a() hudPrint("Hello World") end
function b(dir) local dir = dir or (party.facing+2)%4 party.party:knockback(dir) end
function c() b(party.facing) end
function d(loc) if not loc then loc = {5,17,0,1,1} end if type(loc) == "table" then party:setPosition(unpack(loc)) end end
function e() party.party:turn(1) d({5,17,(party.facing+1)%4,0,1}) end
function f(dir) local dir = dir or 1 party.party:turn(dir) d({party.x,party.y,party.facing,party.elevation,party.level}) end
function g() f(-1) end
function h() party.party:swapChampions(math.random(2), math.random(3,4)) end
function i() playSound("secret") end
function j() damageTile(party.level, party.x, party.y, party.facing, party.elevation, 64, "physical", 2) party.party:shakeCamera(1,1) playSound("party_crushed") end
function k() party.party:heal() end
function l() party.party:rest() end
function m() local dX,dY = getForward(party.facing) local imp = party:spawn("trickster") imp:setPosition(imp.x +dX, imp.y+dY, (imp.facing+2)%4, imp.elevation+1, imp.level) end
function n() counter_1.counter:setValue(0) end
actions = {"a","b","c","d","e","g","h","i","j","k","l","m","n"}
function selectFromCounter()
local val = counter_1.counter:getValue()
if val>0 and val<= #actions then
findEntity(self.go.id).script[actions[val]]()
end
end