Lua script behaves differently from different bundles

Hi, i am making a little simulator for the board game “quoridor”. https://alexarjing.github.io/demo/quoridor/ . Everything runs ok in build from defold editor or bundle to exe. But when i try to bundle it with html-h5, something appears wrong.

ERROR:SCRIPT: invalid key to ‘next’
printErr @ dmloader.js:299
dmloader.js:299 stack traceback:
printErr @ dmloader.js:299
dmloader.js:299 [C]: in function ‘(for generator)’
printErr @ dmloader.js:299
dmloader.js:299 main/game.script:322: in function ‘check_jump’
printErr @ dmloader.js:299
dmloader.js:299 main/game.script:305: in function ‘check_role_path’
printErr @ dmloader.js:299
dmloader.js:299 main/game.script:470: in function ‘check_select’
printErr @ dmloader.js:299
dmloader.js:299 main/game.script:534: in function <main/game.script:529>

Is it forbiden to use “id of a gameobject” or “tostring a table” as the key for a table ? Thanks!

edit:

path = {
 id_of_a_go = something,
 id_of_a_go = something,
 id_of_a_go = something
}
for k, tile in pairs(path) do
---some code here
end

That is totally okay. I just tested this on desktop and html5 without problems.

local t = { [go.get_id("go1")] = "hello",
			[go.get_id("go2")] = "world" }

for k,v in pairs(t) do
	print(k,v)
end

Can you elaborate a bit what you mean when you say you "tostring a table”?

1 Like

for example something like this

local test = {}
local tab[test] = 1
for k,v in pairs(tab) do
---something
end

shall i have to write tab[tostring(test)] instead?

That’s not valid?

local test = {"a","b","c"}
local tab = {1,2,3}
tab[test] = 1

for k,v in pairs(tab) do
  print(k)
end

for k,v in pairs(test) do
  print(v)
end
1	
2	
3	
table: 0x1c361d0	
a	
b	
c

Trying to use tables as keys sounds like a bad idea to me.

Do you want to do something like this?

local test = {"a","b","c"}
local tab = {1,2,3}
tab[table.concat(test,'\0')] = {5,6,7,8}

for k,v in pairs(tab) do
  print(k)
end
print()

for k,v in pairs(tab[table.concat(test,'\0')]) do
  print(v)
end

Are you anywhere relying on the string representation of a hash? I.e are you doing tostring() on an id and then manipulating that string?

function game.check_role_path(role)
	local path = {}
	local tc = role.tile
	local tl = game.tile[role.ix-1] and game.tile[role.ix-1][role.iy]
	local tr = game.tile[role.ix+1] and game.tile[role.ix+1][role.iy]
	local tt = game.tile[role.ix][role.iy-1]
	local tb = game.tile[role.ix][role.iy+1]
	if tc.l and not tc.l.fill and tl then
		path[tl] = tl
	end
	if tc.r and not tc.r.fill and tr then
		path[tr] = tr
	end
	if tc.t and not tc.t.fill and tt then
		path[tt] = tt
		local s = tc.t
	end
	if tc.b and not tc.b.fill and tb then
		path[tb] = tb
	end
	path = game.check_jump(path,role)
	return path
end

game.tile[][] is a table.
i use it just because i wanted to delete some key-value easily during a “for k,v pairs() end” by nil it. Ok…i will change the keys to integer…

if tc.l and not tc.l.fill and tl then
	table.insert(path,tl)
	--path[tl] = tl
end

finally, bug fixed by using new table to restore data instead of adding/removing elements during ergod of a table.

1 Like