Why is this script crashing?

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!
Post Reply
GoldenShadowGS
Posts: 168
Joined: Thu Oct 30, 2014 1:56 am

Why is this script crashing?

Post by GoldenShadowGS »

Code: Select all

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:

Code: Select all

local order = table.concat(comb)  -- invalid value (nil) at index 9 in table for 'concat'
Is this a bug?
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: Why is this script crashing?

Post by Batty »

With comb[r] = nil you place nil in the comb table so maybe do comb[r] = 0 instead?
GoldenShadowGS
Posts: 168
Joined: Thu Oct 30, 2014 1:56 am

Re: Why is this script crashing?

Post by GoldenShadowGS »

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.
GoldenShadowGS
Posts: 168
Joined: Thu Oct 30, 2014 1:56 am

Re: Why is this script crashing?

Post by GoldenShadowGS »

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

Code: Select all

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
Alternate method:

Code: Select all

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
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Why is this script crashing?

Post by minmay »

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.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Post Reply