Need help with item-based combination lock [SOLVED!]
Posted: Mon Jun 13, 2016 4:16 am
Hey! Before I start, I want to say that I have searched the megathread and other sources for answers, but I've been coming up short. Though what I have learned has been a great help generally.
I'm working on a combination lock puzzle. The gist of it is this: the player has six "punch cards" (defined "punchcard" as a new item), ID'd from 1 to 6 (punchcard_1, etc). The goal is for the door to unlock if the player places the proper punch cards on the surface. (Putting something that isn't a punchcard on the surface also gives the player an error message and tells them to restart the puzzle.) I plan on having several puzzles related to these cards, so they're meant to be used repeatedly in other puzzles.
The kicker is that I want the order to matter - card 4, then 2, then 3, or whatever. There's a counter which only increments when the correct cards are placed in the correct order.
So, while the system recognizes the first card placed, and recognizes when all six are placed (which generates an "incorrect passcode" message for the player, since the code is only three numbers long), and even recognizes when a non-punchcard item is placed, it doesn't recognize any subsequent cards placed - it keeps detecting the first one placed and only that one. Because of that, it keeps resetting the counter to 0 so it loses track of the order the cards were placed in, making success impossible. The script is attached to a "castle_wall_tray" asset, if that matters.
If it wasn't for the order requirement, this would be an easy fix, but I haven't been able to find a way to get the script to read the full stack - it always just reads the first one. Sorry if the code's a mess, this is my first foray into LUA and it's been a long while since I worked with anything else.
I'm working on a combination lock puzzle. The gist of it is this: the player has six "punch cards" (defined "punchcard" as a new item), ID'd from 1 to 6 (punchcard_1, etc). The goal is for the door to unlock if the player places the proper punch cards on the surface. (Putting something that isn't a punchcard on the surface also gives the player an error message and tells them to restart the puzzle.) I plan on having several puzzles related to these cards, so they're meant to be used repeatedly in other puzzles.
The kicker is that I want the order to matter - card 4, then 2, then 3, or whatever. There's a counter which only increments when the correct cards are placed in the correct order.
So, while the system recognizes the first card placed, and recognizes when all six are placed (which generates an "incorrect passcode" message for the player, since the code is only three numbers long), and even recognizes when a non-punchcard item is placed, it doesn't recognize any subsequent cards placed - it keeps detecting the first one placed and only that one. Because of that, it keeps resetting the counter to 0 so it loses track of the order the cards were placed in, making success impossible. The script is attached to a "castle_wall_tray" asset, if that matters.
Code: Select all
function identifyPunchcard(surface)
-- determine which punch card was placed on the reader, and returns 0 if the item wasn't a punchcard (currently always reads the first item placed, but I need it to read the most recent item placed)
for v,i in surface:contents() do
if i.go.id == "punchcard_1" then return 1
elseif i.go.id == "punchcard_2" then return 2
elseif i.go.id == "punchcard_3" then return 3
elseif i.go.id == "punchcard_4" then return 4
elseif i.go.id == "punchcard_5" then return 5
elseif i.go.id == "punchcard_6" then return 6
else return 0
end
end
end
function tallyPunchcards(surface)
-- a secondary check, to make sure only those cards that are part of the code are on the reader
local punchCombo = 0
for v,i in surface:contents() do
if i.go.id == "punchcard_1" then punchCombo = punchCombo + 1 end
if i.go.id == "punchcard_2" then punchCombo = punchCombo + 2 end
if i.go.id == "punchcard_3" then punchCombo = punchCombo + 4 end
if i.go.id == "punchcard_4" then punchCombo = punchCombo + 8 end
if i.go.id == "punchcard_5" then punchCombo = punchCombo + 16 end
if i.go.id == "punchcard_6" then punchCombo = punchCombo + 32 end
if i.go.name ~= "punchcard" then return 0 end
end
return punchCombo
end
function checkPasscode()
--main function checks each card as it is placed and increments the counter if the card is the correct one in the sequence, then checks to see if the unlock conditions are met
if identifyPunchcard(passcode_tray_storehouse.surface) == 1
then storehouse_password_count.counter:setValue(0)
hudPrint("Entered 1")
elseif identifyPunchcard(passcode_tray_storehouse.surface) == 2
then hudPrint("Entered 2")
if storehouse_password_count.counter:getValue() == 1
then storehouse_password_count.counter:increment()
else storehouse_password_count.counter:setValue(0)
end
elseif identifyPunchcard(passcode_tray_storehouse.surface) == 3
then hudPrint("Entered 3")
storehouse_password_count.counter:setValue(0)
if storehouse_password_count.counter:getValue() == 2
then storehouse_password_count.counter:increment()
else storehouse_password_count.counter:setValue(0)
end
elseif identifyPunchcard(passcode_tray_storehouse.surface) == 4
then hudPrint("Entered 4")
if storehouse_password_count.counter:getValue() == 0
then storehouse_password_count.counter:increment()
else storehouse_password_count.counter:setValue(0)
end
elseif identifyPunchcard(passcode_tray_storehouse.surface) == 5
then hudPrint("Entered 5")
storehouse_password_count.counter:setValue(0)
elseif identifyPunchcard(passcode_tray_storehouse.surface, "punchcard_6") == 6
then hudPrint("Entered 6")
storehouse_password_count.counter:setValue(0)
else storehouse_password_count.counter:setValue(0)
end
if storehouse_password_count.counter:getValue() == 3 and tallyPunchcards(passcode_tray_storehouse.surface) == 14
then hudPrint("Passcode accepted!")
storehouse_door.door:open()
elseif tallyPunchcards(passcode_tray_storehouse.surface) == 63
then party.party:shakeCamera(0.02, 1)
hudPrint("Passcode denied!")
elseif tallyPunchcards(passcode_tray_storehouse.surface) == 0
then hudPrint("Error! Non-punchcard item detected. Please remove all items and try again.")
end
end