open a door requiring three keys not in any order.
Assign this to the key: Call this file keymulti3.lua
-- LUA Script - precede every function and global member with lowercase name of script + '_main'
-- Player Collects Key
doorkey = {}
function keymulti3_init(e)
doorkey = 0
end
function keymulti3_main(e)
PlayerDist = GetPlayerDistance(e)
if PlayerDist < 150 then
SourceAngle = g_PlayerAngY
while SourceAngle < 0.0 do
SourceAngle = SourceAngle + 360.0
end
while SourceAngle > 340.0 do
SourceAngle = SourceAngle - 360.0
end
PlayerDX = (g_Entity[e]['x'] - g_PlayerPosX)
PlayerDZ = (g_Entity[e]['z'] - g_PlayerPosZ)
DestAngle = math.atan2( PlayerDZ , PlayerDX )
-- Convert to degrees
DestAngle = (DestAngle * 57.2957795) - 90.0
Result = math.abs(math.abs(SourceAngle)-math.abs(DestAngle))
if Result > 180 then
Result = 0
end
if Result < 20.0 then
--[[
SourceAngle = g_PlayerAngX
while SourceAngle < 0.0 do
SourceAngle = SourceAngle + 360.0
end
while SourceAngle > 335.0 do
SourceAngle = SourceAngle - 360.0
end
DestAngle = math.atan2( PlayerDZ , PlayerDY )
-- Convert to degrees
DestAngle = (DestAngle * 57.2957795) - 90.0
Result = math.abs(math.abs(SourceAngle)-math.abs(DestAngle))
if Result > 180 then
Result = 0
end
if Result < 25.0 then
--]]
Prompt ("Press E To pick up key")
if g_KeyPressE == 1 then
PromptDuration("Collected key",3000)
PlaySound(e,0)
Collected(e)
Destroy(e)
doorkey = doorkey + 1
--ActivateIfUsed(e)
end
--end
end
Prompt ("Keys obtained =" .. doorkey)
end
end
Here is the door script: Call this script: doormultikey3.lua Dont forget to put the name of the key in the haskey field.
-- LUA Script - precede every function and global member with lowercase name of script + '_main'
-- Door Prompts 'Closed' can be opened with entity collected specified by 'USE KEY'
-- state to ensure user must release E key before can open/close again
door_pressed = 0
doorkey = 0
keys_remaining = 0
function doormultikey3_init(e)
end
function doormultikey3_main(e)
PlayerDist = GetPlayerDistance(e)
if PlayerDist < 100 and g_PlayerHealth > 0 then
GetEntityPlayerVisibility(e)
if g_Entity[e]['plrvisible'] == 1 then
if g_Entity[e]['activated'] == 0 then
if g_Entity[e]['haskey'] == -1 then
SetActivated(e,1)
else
if g_Entity[e]['haskey'] == 1 and
doorkey == 3 then
Prompt("The door is locked. Press E key to unlock door")
if g_KeyPressE == 1 then
SetActivated(e,1)
end
else
keys_remaining = 3 - doorkey
Prompt("The door is locked. Find " .. keys_remaining .." more doorkey keys to unlock door")
end
end
else
if g_Entity[e]['activated'] == 1 then
-- door is unlocked and closed
Prompt("Press E to open door")
if g_KeyPressE == 1 and g_Entity[e]['animating'] == 0 and door_pressed == 0 then
SetAnimation(0)
PlayAnimation(e)
g_Entity[e]['animating'] = 1
SetActivated(e,2)
ActivateIfUsed(e)
PlaySound(e,0)
StartTimer(e)
door_pressed = 1
end
else
if g_Entity[e]['activated'] == 2 then
-- door collision off after 1 second
if GetTimer(e)>1000 then
CollisionOff(e)
end
-- door is open
if g_KeyPressE == 1 and g_Entity[e]['animating'] == 0 and door_pressed == 0 then
SetAnimation(1)
PlayAnimation(e)
g_Entity[e]['animating'] = 1
SetActivated(e,1)
PlaySound(e,1)
CollisionOn(e)
door_pressed = 1
end
end
end
end
end
end
if g_KeyPressE == 0 then
door_pressed = 0
end
end
I have tested this and it works, Let me know how it does for you. Thanks to smallg for the initial idea.
Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz, 2400 Mhz, 4 Core(s), 4 Logical Processor(s), 8gb RAM, Nvidia gtx660, Windows 7 Pro 64bit