Page 1 of 3

Scripting a Pressure Plate puzzle - Help!

Posted: Thu Jun 20, 2013 10:08 am
by Sir Bubbles
Hey Grimrock modding section, this is my first post here and I apologize if a thread has already been made for this exact problem. I've been wanting to set up a simple sequence puzzle in my dungeon consisting of 6 pressure plates, which when stepped on in the proper order, opens a door. Now I have basically no experience with scripting whatsoever, but I have been trying to learn for the sake of making my own Grimrock dungeons. So because of this I don't even know where to start really and would need to be spoon-fed every little detail and instruction on how to execute this. I would greatly appreciate some help, or at least a nudge in the right direction so I can (try to) figure this out myself.

Re: Scripting a Pressure Plate puzzle - Help!

Posted: Thu Jun 20, 2013 3:18 pm
by Komag
That's a bit advanced, so you'll really want to just take it slow, watch all the videos, practice very simple things first, till you learn more and more over the days and weeks

Re: Scripting a Pressure Plate puzzle - Help!

Posted: Thu Jun 20, 2013 3:43 pm
by Sir Bubbles
Yeah I suppose so. I've been trying to teach myself, managed to set up a few very simple scripts so far. I'll keep working at it I guess.

Re: Scripting a Pressure Plate puzzle - Help!

Posted: Thu Jun 20, 2013 4:42 pm
by Ryeath_Greystalk
Komag is correct in that it can get more involved than it might appear. But just to point you in a general direction you will need,

1) a method to record whether or not a pressure plate has been triggered, possibly a counter or connect it to an inaccessible wall lever.
2) a script which will,
A) check if plate 1 has been pressed and,
1) if yes go to the next plate
2) if no reset the puzzle
You may also want to give some indication that the player has the selection correct up til the point of failure. For instance a light above each plate which will turn on when activated in the correct order, but all will turn off when the incorrect plate is activated.

Good luck with your dungeon

Re: Scripting a Pressure Plate puzzle - Help!

Posted: Thu Jun 20, 2013 10:54 pm
by msyblade
Grimwold graced us with many generic prefab scripts to do just these types of things for you, you just need to go in and change the names to your objects. I believe the one you want is "Plate Path Sequence".

viewtopic.php?f=14&t=4564

If you are absolutely new to scripting, it may seem quite complex to you, in which case your only choice will be to follow Komag and Ryeaths advice and move on with your dungeon and become more fimiliar with basic functions, then come back to this area later, when the scripts make some sense to you.

Edit: You will find many helpful scripts here:
viewtopic.php?f=14&t=3099

Re: Scripting a Pressure Plate puzzle - Help!

Posted: Fri Jun 21, 2013 4:08 am
by Kein Mitleid
Whipped up some code for you, Bubbles.
Tested it out and it works, but since I'm a bit new myself, I wouldn't mind if someone found any flaws or bad practices in my code.

Code: Select all

counter = 0

-- insert name of door within quotes
puzzle_door_name = "INSERT NAME HERE"

-- insert names of plates, in order, within brackets
puzzle_plates = { "INSERT NAMES HERE" }

for i = 1, #puzzle_plates do
	local current_plate = findEntity(puzzle_plates[i])
	current_plate:addConnector("activate", self.id, "isPressed")
	current_plate:setTriggeredByParty(true)
	current_plate:setTriggeredByMonster(false)
	current_plate:setTriggeredByItem(false)
end

function isPressed()   
	counter = counter + 1
	local correct_plate = findEntity(puzzle_plates[counter])
	local puzzle_door = findEntity(puzzle_door_name)

	if not correct_plate:isDown() then
		counter = 0
	end

	if counter == #puzzle_plates then
		puzzle_door:open()
		counter = 0
	end
end

Re: Scripting a Pressure Plate puzzle - Help!

Posted: Fri Jun 21, 2013 9:31 pm
by Ryeath_Greystalk
Kein Mitleid wrote:Whipped up some code for you, Bubbles.
Tested it out and it works, but since I'm a bit new myself, I wouldn't mind if someone found any flaws or bad practices in my code.

Code: Select all

counter = 0

-- insert name of door within quotes
puzzle_door_name = "INSERT NAME HERE"

-- insert names of plates, in order, within brackets
puzzle_plates = { "INSERT NAMES HERE" }

for i = 1, #puzzle_plates do
	local current_plate = findEntity(puzzle_plates[i])
	current_plate:addConnector("activate", self.id, "isPressed")
	current_plate:setTriggeredByParty(true)
	current_plate:setTriggeredByMonster(false)
	current_plate:setTriggeredByItem(false)
end

function isPressed()   
	counter = counter + 1
	local correct_plate = findEntity(puzzle_plates[counter])
	local puzzle_door = findEntity(puzzle_door_name)

	if not correct_plate:isDown() then
		counter = 0
	end

	if counter == #puzzle_plates then
		puzzle_door:open()
		counter = 0
	end
end
Hello Kein,

That is some really nifty coding there. I've spent about twenty minutes going over it and I think I finally have it figured out (very new to scripting myself).

So if I have it figured out correctly you are taking all the plates and putting them in a table and then making all the settings, thus if a person wanted to add or remove plates it can be done via editing the table.(all done at start up because it's not in a function, correct???)

Then when a plate calls the function it retrieves the plate info from the table, based on the counter value, and if that is the plate that is down it checks for success, otherwise it resets. Then if the counter reaches the same number has the amount of plates in the table it opens the door.

So, did I get it right?

Really compact. I would have just bulldozed my way through with a bunch of if then statements.

Re: Scripting a Pressure Plate puzzle - Help!

Posted: Fri Jun 21, 2013 10:20 pm
by Sir Bubbles
My friend Kein has been helping me code puzzles in my dungeon so first of all thanks a million for that. But we've seem to hit a bump here, he got the puzzle working for me in a simple 6 press sequence, where none of the same buttons are pressed twice, and the door opens. However, when we try to expand on that, making it so the player must step on the same button multiple times, it glitches out and apparently the buttons are getting pressed twice when stepped on only once. Does anyone know what the cause or possible fix is for this?

Here's the script:

Code: Select all

counter = 0

-- insert name of door within quotes
puzzle_door_name = "puzzle_door"

-- insert names of plates, in order, within brackets
puzzle_plates = { "puzzle_plate_a_5",
"puzzle_plate_a_6",
"puzzle_plate_a_5",
"puzzle_plate_a_2",
"puzzle_plate_a_1",
"puzzle_plate_a_4",
"puzzle_plate_a_1",
"puzzle_plate_a_2",
"puzzle_plate_a_3",
"puzzle_plate_a_2" }

for i = 1, #puzzle_plates do
local current_plate = findEntity(puzzle_plates[i])
current_plate:addConnector("activate", self.id, "isPressed")
current_plate:setTriggeredByParty(true)
current_plate:setTriggeredByMonster(false)
current_plate:setTriggeredByItem(false)
end

function isPressed()   
counter = counter + 1
local correct_plate = findEntity(puzzle_plates[counter])
local puzzle_door = findEntity(puzzle_door_name)

if counter == #puzzle_plates then
puzzle_door:open()
counter = 0
end

hudPrint("Down: " .. tostring(correct_plate:isDown()) .. " Up: " .. tostring(correct_plate:isUp()))
end

Re: Scripting a Pressure Plate puzzle - Help!

Posted: Fri Jun 21, 2013 11:10 pm
by Ryeath_Greystalk
Not sure if this is a cause of your problem or not but in the set up portion you are adding multiple connectors to the pressure plate. You might need a second table just for initialization.

Re: Scripting a Pressure Plate puzzle - Help!

Posted: Fri Jun 21, 2013 11:12 pm
by Sir Bubbles
Yep that was actually the problem. He told me he accidentally put a couple of connections between some plates. Removing them fixed the problem, thanks for the response though.