lab
lab copied to clipboard
How can I programmatically open/close doors?
I am trying to programmatically control when doors are open (and stay open) and closed (and stay closed/locked). More generally, I want to be able to take information from events in the environment, play with it, and then change the environment.
I am confused how to exactly, say, force a door to be (and stay) open. I can see that, through the api:updateSpawnVars
function, we can get the attributes for 'func_door', but that only includes angle classname, targetname, model
, but none of these seem to indicate the state of the door, other than some angle.
Some files indicate that I should be able to set api._doorsOpened[targetname] = true
but that doesn't seem to work, and setting spawnVars.wait = '-1'
doesn't seem to allow keeping a door closed on command (or making it open without player interaction). I don't understand how these properties are used/called by the game.
function api:canTrigger(entityId, targetName)
return not api._doorsOpened[targetName] and
api._carriedKeyColor == api._doors[targetName]
end
function api:trigger(entityId, targetName)
if api._doors[targetName] and not api._doorsOpened[targetName] then
api._doorsOpened[targetName] = true
api._carriedKeyColor = nil
game:addScore(1)
end
end
function api:updateSpawnVars(spawnVars)
local classname = spawnVars.classname
if classname == 'key' then
api._keysCount = api._keysCount + 1
spawnVars.classname = KEY_PREFIX .. api._keys[api._keysCount]
spawnVars.id = tostring(api._keysCount)
spawnVars.wait = "-1" -- set to -1 so that key will not respawn
end
if classname == 'func_door' then
api._doorsCount = api._doorsCount + 1
spawnVars.id = tostring(api._doorsCount + 100)
spawnVars.wait = "1000000" --[[ sufficient large number to make the door
open forever once it has been opened.]]
api._doors[spawnVars.targetname] = api._level.doors[api._doorsCount]
end
if classname == 'goal' then
spawnVars.id = tostring(GOAL_ID)
spawnVars.wait = "-1"
end
return spawnVars
end
If I am misunderstanding a general design pattern or paradigm in the code that will help me solve the above problem and others more generally, please let me know.