Re: [Help] Scripting a combination lock with buttons.
Posted: Fri Oct 12, 2012 2:39 pm
Ok then, it may look cumbersome and can get worse but it serves its purpose:
Firstly you have to spawn some buttons and connect them via script (this save lots of connections in the map view).
Secondly you have to create a function with the argument (sender) and then start the clever part.
Every if statement covers one possibility, so if there are three buttons then there will be three possibilities each, pressed first, second or third.
Now it becomes very simple to script which one will be the first, the second and the third, you just have to get the counter value and decrease it only if the button is pressed in the correct sequence.
To make it simple, if you want to press the first button for last you have to decrease the counter only when it is at the value of 1 and reset it in all the other cases.
Lastly create a single counter, set its value to the number of buttons you have and connect it to the script with activate.
With this method there is no need for a reset switch or multiple counters.
Here is an example:
Firstly you have to spawn some buttons and connect them via script (this save lots of connections in the map view).
Secondly you have to create a function with the argument (sender) and then start the clever part.
Every if statement covers one possibility, so if there are three buttons then there will be three possibilities each, pressed first, second or third.
Now it becomes very simple to script which one will be the first, the second and the third, you just have to get the counter value and decrease it only if the button is pressed in the correct sequence.
To make it simple, if you want to press the first button for last you have to decrease the counter only when it is at the value of 1 and reset it in all the other cases.
Lastly create a single counter, set its value to the number of buttons you have and connect it to the script with activate.
With this method there is no need for a reset switch or multiple counters.
Here is an example:
Code: Select all
spawn("wall_button", level, x, y, facing, "button_1"):addConnector("toggle", "script", "buttonPress")
spawn("wall_button", level, x, y, facing, "button_2"):addConnector("toggle", "script", "buttonPress")
spawn("wall_button", level, x, y, facing, "button_3"):addConnector("toggle", "script", "buttonPress")
--this example will have a combination of: 2 3 1
function buttonPress(sender)
if sender.id == "button_1" and counter:getValue() == 3 then
counter:setValue(3)
elseif sender.id == "button_1" and counter:getValue() == 2 then
counter:setValue(3)
elseif sender.id == "button_1" and counter:getValue() == 1 then
counter:setValue(0)
elseif sender.id == "button_2" and counter:getValue() == 3 then
counter:setValue(2)
elseif sender.id == "button_2" and counter:getValue() == 2 then
counter:setValue(3)
elseif sender.id == "button_2" and counter:getValue() == 1 then
counter:setValue(3)
elseif sender.id == "button_3" and counter:getValue() == 3 then
counter:setValue(3)
elseif sender.id == "button_3" and counter:getValue() == 2 then
counter:setValue(1)
elseif sender.id == "button_3" and counter:getValue() == 1 then
counter:setValue(3)
elseif counter:getValue() == 0 then
door:open() -- or whatever you want to do
end
end