Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
combination = 44441133323
comb = {}
count = 0
solved = false
function buttonCombination(self)
--if solved == false then
count = count + 1
for i = 1,4 do
if string.match(self.go.id, i) then
comb[count] = i
local order = table.concat(comb)
local s, b = string.find(combination, order)
if s == 1 and b == count and count < string.len(combination) then
playSound("key_lock")
elseif s == 1 and b == string.len(combination) then
playSound("key_lock")
dungeon_door_portcullis_1.door:open()
solved = true
else
playSound("lock_incorrect")
for r = 1,string.len(combination) do
comb[r] = nil
end
count = 0
end
end
end
--end
end
This is the combination script I just wrote for the help thread. If I don't use solved variable to prevent the function from working, the second time you try to enter the combination, the script crashes with this error:
Why does it always crash at index 9 on the 2nd attempt at solving the combination? I can't figure it out. If nil was causing a crash, it would happen on the first click.
I figured out what was going on. An extra index was being added after the puzzle was solved so I have a logic check so the script stops if the count is greater than the combination length
combination = 44441133323
comb = {}
count = 0
solved = false
function buttonCombination(self)
--if solved == false then
count = count + 1
for i = 1,4 do
if string.match(self.go.id, i) then
print(count)
print(string.len(combination))
if count <= string.len(combination) then
comb[count] = i
local order = table.concat(comb)
local s, b = string.find(combination, order)
print(s,b)
print(order)
if s == 1 and b == count and count < string.len(combination) then
playSound("key_lock")
elseif s == 1 and b == string.len(combination) then
playSound("key_lock")
dungeon_door_portcullis_1.door:open()
solved = true
else
playSound("lock_incorrect")
for r = 1,string.len(combination) do
comb[r] = nil
end
count = 0
dungeon_door_portcullis_1.door:close()
end
else
playSound("lock_incorrect")
for r = 1,string.len(combination) do
comb[r] = nil
end
count = 0
dungeon_door_portcullis_1.door:close()
end
end
end
--end
end
combination = 44441133323
comb = {}
count = 0
solved = false
function buttonCombination(self)
--if solved == false then
count = count + 1
for i = 1,4 do
if string.match(self.go.id, i) then
comb[count] = i
local fullorder = table.concat(comb)
local order = string.sub(fullorder,1,count)
local s, b = string.find(combination, order)
print(s,b)
print(fullorder)
print(order)
if s == 1 and b == count and count < string.len(combination) then
playSound("key_lock")
elseif s == 1 and b == string.len(combination) then
playSound("key_lock")
dungeon_door_portcullis_1.door:open()
solved = true
else
playSound("lock_incorrect")
for r = 1,string.len(combination) + 1 do
comb[r] = 0
end
count = 0
dungeon_door_portcullis_1.door:close()
end
end
end
--end
end
GoldenShadowGS wrote:Why does it always crash at index 9 on the 2nd attempt at solving the combination? I can't figure it out. If nil was causing a crash, it would happen on the first click.
table.concat goes up to the highest integer key; otherwise,how would it know when to stop? As you can see in the Lua reference manual, it requires that all elements be numbers or strings. So if there's a nil value in the middle of the "array" you'll get an error, the same as you would if you had a function or table in the middle of the table.