I am trying to control weather a trigger_delay is fired or not based on the state of a door. This is to prevent the delay from opening a door that is already closed.
Problem: When a door is opened the trigger_delay is fired immediately, not after the delay duration.
- When a door is first opened the delay is triggered and permitted
- If the user then closes the door, the second delay trigger is interrupted, but the first one still goes through. This causes the door to still be opened when it was closed.
Here's the code I have so far:
if doors==nil then doors={} end
totalDoors = 35 -- total number of doors in the map
bInterrupt = false -- controls if the trigger will be permitted
bDebug = true -- set true to print values to console for testing
-- naming convention:
-- doors: d1, d2, d3...
-- trigger_delay: td1, td2, td3...
function initArray(m)
local array = {}
for i = 1, m do
array[i] = {d = "d"..i, td = "td"..i, open = false}
end
return array
end
doors=initArray(totalDoors)
addhook("triggerentity","TriggerUsed")
function TriggerUsed(x, y) -- entity triggered
bInterrupt = false -- dont interrupt unless we tell it to
sName = entity(x,y,"name")
-- loop thru doors array
for i = 1, totalDoors do
aDelay = doors[i].td
aDoor = doors[i].d
if sName == aDelay then -- the trigger is a trigger_delay
if bDebug == true then -- print name and state as string to console
if doors[i].open == true then sState = "Yes" end
if doors[i].open == false then sState = "No" end
print(sName.." was triggered: Door Open? :"..sState)
end
if doors[i].open == false then -- door is closed
bInterrupt = true -- dont open it
i = totalDoors -- exit the loop and continue
if bDebug == true then print("***"..sName.." : door is already closed!***") end
else -- the door is open
bInterrupt = false -- let it close
i = totalDoors -- exit the loop and continue
if bDebug == true then print("***"..sName.." : door was OPEN, closing...***") end
end
elseif sName == aDoor then -- the trigger is a door
bState = entity(x,y,"state")
if bDebug == true then -- print name and state as string to console
if bState == true then sState = "Yes" end
if bState == false then sState = "No" end
print(sName.." was triggered: Door Open? :"..sState)
end
if bState == true then -- the door is open
bInterrupt = false -- allow it to close
doors[i].open = false -- set array item state to closed
i = totalDoors -- exit the loop and continue
if bDebug == true then print(sName.." was OPEN, closing..") end
else -- the door is closed
bInterrupt = false -- allow it to open
doors[i].open = true -- set array item state to open
i = totalDoors -- exit the loop and continue
if bDebug == true then print(sName.." was CLOSED, openning...") end
end
end
end
-- should the trigger be triggered or not?
if bInterrupt == true then
if bDebug == true then print(sName.." was Interrupted") end
return 1 -- do not trigger
else
if bDebug == true then print(sName.." was permitted") end
return 0 -- trigger as usual
end
end
With this code if I open and then close a door it produced the following results:
[10:11:54] d6 was triggered: Door Open? :No <- I opened the door
[10:11:54] d6 was CLOSED, openning...
[10:11:54] d6 was permitted
[10:11:54] s6 was permitted
[10:11:54] td6 was triggered: Door Open? :Yes
[10:11:54] ***td6 : door was OPEN, closing...***
[10:11:54] td6 was permitted
[10:11:54] d6 was triggered: Door Open? :Yes <- I closed the door
[10:11:54] d6 was OPEN, closing..
[10:11:54] d6 was permitted
[10:11:54] s6 was permitted
[10:11:54] td6 was triggered: Door Open? :No
[10:11:54] ***td6 : door is already closed!***
[10:11:54] td6 was Interrupted <- the second delay is canceled
[10:11:57] d6 was triggered: Door Open? :No <- the first delay still opens
[10:11:57] d6 was CLOSED, openning... the door
[10:11:57] d6 was permitted
[10:11:57] s6 was permitted
Any idea on how to do this properly? It seems it only detects the state when the delay is first triggered and not after the delay interval. Any help would be appreciated.