[Help] Scripting a combination lock with buttons.

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Custode
Posts: 15
Joined: Wed Oct 10, 2012 8:29 pm

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

Post 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
Last edited by Custode on Sat Oct 13, 2012 3:57 am, edited 1 time in total.
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

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

Post 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
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

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

Post by Komag »

yes, that is a good one, very unique, definitely worth of the script repository! :)
Finished Dungeons - complete mods to play
Custode
Posts: 15
Joined: Wed Oct 10, 2012 8:29 pm

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

Post by Custode »

Oh thanks! I always like to experiment and find different solutions in scripting.
Grajak
Posts: 1
Joined: Mon Sep 22, 2014 4:31 pm

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

Post 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
Post Reply