@smallg thanks for that, it does work for both switches and I love the extra function that checks to see if you are looking at the switch but I have animation on the switch aswell.
I have been working on this myself for the last couple of days and came up with a solution that works great. Here is the script for the door and the switch.
Animated door and Switch script. The pause for the door opening and closing needs to be changed for different doors.
Door:
g_DoorE = {}
g_DoorName = {}
function huge_door_init_name(e,name)
g_DoorE[e] = e
g_DoorName[e] = name
end
function huge_door_main(e)
end
Switch:
-- This is the code to open and close a door that has two switches..
g_dooropen = 0 -- 0 means that the door is closed..
g_pausetime = 13000 -- Time it takes for the door to open / close..
g_firsttime = 0 -- 0 Means that this is the first time it has been used..
g_pressed = 0
g_SwitchE = {} -- The Array of switches in this case there will be two..
g_Switch_Name = {}
local DoorEntity = 0
local FirstSwitch = 0
local SecondSwitch = 0
function multiswitch_init_name(e, name)
-- Globals to keep the name and entity number of this object..
g_Switch_Name[e] = name
g_SwitchE[e] = e
-- Get the number of the door..
DoorEntity = get_door_entity_number(name)
-- This becomes the first switch..
FirstSwitch = e
-- This becomes the second switch..
SecondSwitch = check_for_second_switch(e, name)
end
function multiswitch_main(e)
if GetPlayerDistance(e) <= 100 then
-- The player is close enough to the switch..
if g_Entity[DoorEntity] ~= nil and g_Entity[FirstSwitch] ~= nil and g_Entity[SecondSwitch] ~= nil then
-- All entity's have been placed..
if g_dooropen == 0 then
-- The door is closed..
if GetTimer(DoorEntity) >= g_pausetime or g_firsttime == 0 then
-- This is either the first time or the pause time has been reached..
Prompt("Press E To Open")
if g_KeyPressE == 1 and g_pressed == 0 then
-- The 'E' key has been pressed..
-- Setup the variables needed next, play the animations for each entity and start the Timer..
SetAnimation(0)
PlayAnimation(FirstSwitch)
PlayAnimation(SecondSwitch)
PlayAnimation(DoorEntity)
CollisionOff(DoorEntity)
g_pressed = 1
g_dooropen = 1 -- Door Open..
StartTimer(DoorEntity)
if g_firsttime == 0 then
-- If this was the first time then reset this variable to tell th script that it is not anymore..
g_firsttime = 1
end
end
else
-- This will show while it is waiting for the door to close..
Prompt("Door Closing and Locking")
end
else
-- The door is open..
if GetTimer(DoorEntity) >= g_pausetime then
-- The pause time has been reached..
Prompt("Press E to close")
if g_KeyPressE == 1 and g_pressed == 0 then
-- The 'E' key has been pressed..
-- Setup the variables needed next, play the animations for each entity and start the Timer..
SetAnimation(1)
PlayAnimation(FirstSwitch)
PlayAnimation(SecondSwitch)
PlayAnimation(DoorEntity)
CollisionOn(DoorEntity)
g_pressed = 1
g_dooropen = 0 -- Door Closed..
StartTimer(DoorEntity)
end
else
-- This will shile while the door is openening..
Prompt("Door Unlocking and Opening")
end
end
else
-- This will only show if it does not find all the entity's needed..
CurrentPrompt = ""
if g_Entity[DoorEntity] == nil then
CurrentPrompt = CurrentPrompt .. ", " .. "The Door is Missing"
end
if g_Entity[FirstSwitch] == nil then
CurrentPrompt = CurrentPrompt .. ", " .. "The FirstSwitch is Missing"
end
if g_Entity[SecondSwitch] == nil then
CurrentPrompt = CurrentPrompt .. ", " .. "The SecondSwitch is Missing"
end
Prompt(CurrentPrompt)
end
end
-- Reset the Global variable g_pressed..
if g_pressed == 1 and g_KeyPressE == 0 then
g_pressed = 0
end
end
-- Find the door entity based on the name of this entity..
function get_door_entity_number(name)
currentE = 0
for a = 1, 9999 do
if name == g_DoorName[a] then
-- If the name passed in is the same as this name then
-- stop looking and pass this entity number out..
currentE = a
break
end
end
return currentE
end
-- Find the second switch entity based on the name of this entity..
function check_for_second_switch(e, name)
secondswitchE = 0
for a = 1, 9999 do
if name == g_Switch_Name[a] and e ~= g_SwitchE[a] then
-- If the name passed in is the same as this name and it is not the same entity then
-- stop looking and pass this entity number out..
secondswitchE = a
break
end
end
return secondswitchE
end
Thanks again for the help
HP Pavilion Laptop, AMD A8-4555M APU with Radeon(tm) Graphics HD Graphics 1.6GHz, 8GB Memory, 64 Bit Operating System (Windows 10), 1 TB Hard Drive.
I've got something to say - It's better to burn out than fade away.