Page 2 of 2

Re: [Help] Scripting a combination lock with buttons.

Posted: Fri Oct 12, 2012 2:39 pm
by Custode
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:

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

Re: [Help] Scripting a combination lock with buttons.

Posted: Fri Oct 12, 2012 2:50 pm
by Grimwold
Great script. thanks for posting.

It's interesting that we seem to have created two different types of combination lock!

1) Press these buttons in the right order

2) Press each of these button a set number of times

Re: [Help] Scripting a combination lock with buttons.

Posted: Fri Oct 12, 2012 5:00 pm
by Komag
yes, that is a good one, very unique, definitely worth of the script repository! :)

Re: [Help] Scripting a combination lock with buttons.

Posted: Sat Oct 13, 2012 1:12 am
by Custode
Oh thanks! I always like to experiment and find different solutions in scripting.

Re: [Help] Scripting a combination lock with buttons.

Posted: Mon Sep 22, 2014 4:42 pm
by Grajak
Similar to Cuscode's example but requires less of typing :P

Code: Select all

spawn("dungeon_pressure_plate", self.level, 22, 1, 0, "pl1"):addConnector("activate", "script1", "platePressed")
spawn("dungeon_pressure_plate", self.level, 24, 2, 0, "pl2"):addConnector("activate", "script1", "platePressed")
spawn("dungeon_pressure_plate", self.level, 22, 3, 0, "pl3"):addConnector("activate", "script1", "platePressed")


-- proper combination is: pl2 - pl1 - pl2

function platePressed(sender)
	if sender.id == "pl2" and ctr:getValue() == 0 then
		ctr:increment()
	end
	if sender.id == "pl2" and ctr:getValue() ~= 1 then
		ctr:reset()
	end
	
	if sender.id == "pl1" and ctr:getValue() == 1 then
		ctr:increment()
	end 
	if sender.id == "pl1" and ctr:getValue() ~= 2 then
		ctr:reset()
	end
	
	if sender.id == "pl3" and ctr:getValue() == 2 then
		dungeon_door_wooden_3:open() -- or whatever return
	end
	if sender.id == "pl3" and ctr:getValue() ~= 3 then
		ctr:reset()
	end
end