Following, I present you my new and greatly improved code:
Instructions:
Create a counter next to the a lua script_entity and set it to the number of inputs you want the player to make. The activation entities (e.g. buttons or levers) need to be named in the same pattern e.g. sequenceLock_1 to sequenceLock_4.
Name the counter you just placed myCustomSequenceCounter (or anything you wish, but alter that then in the code below). On activation of myCustomSequenceCounter, tell the door to open. Also add an additional connector to the counter itself so it resets after reaching zero.
Then connect each activation entity to the following lua script entity action on the "activate" event:
Code: Select all
function sequenceRiddle()
-- change the following to what entity type you want to use for your riddle
local triggerEntity = "lever"
-- all entity IDs of your activation entities should start with the below, followed by a number
local namingPattern = "sequenceLock_"
-- define the sequence in which you want your entities to be activated
local sequenceOfThings = {"1","2","4","2","3"}
-- define the ID of your sequenceCounter
-- important: this sequence counter needs to have the same initial value
-- as the number of sequences you defined above
local sequenceCounterID = "myCustomSequenceCounter"
-- from here on, it shouldn't be needed to alter the rest of the code
local curSeq = #sequenceOfThings+1-sequenceCounter:getValue()
for i in entitiesAt(party.level, party.x, party.y) do
if i.name == triggerEntity then
if i.id == namingPattern..sequenceOfThings[curSeq] then
findEntity(sequenceCounterID):decrement()
else
findEntity(sequenceCounterID):reset()
end
end
end
end
The downside remains, that there mustn't be multiple activation entities in one square (like one on each side of the square) or else the lock will not work.