Forum

> > CS2D > Scripts > [Lua Problem] Weird table issue
Forums overviewCS2D overview Scripts overviewLog in to reply

English [Lua Problem] Weird table issue

10 replies
To the start Previous 1 Next To the start

old [Lua Problem] Weird table issue

EngiN33R
Moderator Off Offline

Quote
So here's a code snippet:
1
2
3
4
5
6
7
8
9
10
addhook("kill","repchange")
function repchange(id,src)
	reputation[src][faction[id]]=reputation[src][faction[id]]-1
	for _,f in ipairs(factions[faction[id]].allies) do --line 125
		reputation[src][allies[f]]=reputation[src][allies[f]]-0.5
	end
	for _,f in ipairs(factions[faction[id]].enemies) do
		reputation[src][factions[faction[id]].enemies[f]]=reputation[src][factions[faction[id]].enemies[f]]-0.5
	end
end
After I kill a bot with ID 2 whose faction is 4 (faction[2]==4), it gives me the following error:
CS2D Console has written
LUA ERROR: sys/lua/space_rpg/main.lua:125: bad argument #1 to ipairs (table expected, got nil)


The table:

1
2
3
4
5
6
7
8
9
10
11
factions = {
--........
[4] = {
        name = "TeleTec Inc.",
        initrep = 10,
        tag = "[TEL]",
	allies = {1,2,3},
	enemies = {5},
},
--........
}

I have NO idea what's wrong with this thing. Halp?

old Re: [Lua Problem] Weird table issue

Lee
Moderator Off Offline

Quote
1
for _,f in ipairs(factions[faction[id]].allies) do

The inner parameter should be factions[faction[id]].tag.allies

1
for _,f in ipairs(factions[faction[id]].enemies) do

Same here, .tag.enemies

PS: tables are always held as references, so if you alias a table, IE:

1
2
original = {1, 2, 3}
alias = original

Then all changes made on the alias will reflect back in the original as both references the same table in memory. This makes it easy to have temporary variables for deep nested tables:

1
2
local e = factions[faction[id]].enemies
...

old Re: [Lua Problem] Weird table issue

EngiN33R
Moderator Off Offline

Quote
But factions[faction[id]].tag is one thing and factions[faction[id]].allies and enemies are completely another things. The post parser thing just messed up me tabs. .tag is "[TEL]" (string) and .allies and .enemies are different tables.

And I tried the local tables thingy, it doesn't work either.

old Re: [Lua Problem] Weird table issue

Lee
Moderator Off Offline

Quote
Well, that's strange, since your error is

1
LUA ERROR: sys/lua/space_rpg/main.lua:125: bad argument #1 to ipairs (table expected, got nil)
, this means that the index was valid, meaning that factions[faction[id]] is a table. Can you post your entire structure up?


If you're hand coding the factions table, can you post the entire table? If not, one of the other operations most likely corrupted the structure of your table.

Add https://github.com/leegao/see.lua/raw/master/see.lua into the root cs2d folder and at the top of your script, add

1
2
3
4
5
6
7
8
9
require "see"

local __old_print = print
function print(...)
	local args = {...}
	for _,text in ipairs(args) do
		__old_print(tostring(text))
	end
end

and right before line 125, add

1
see(factions)

and post what's printed out

old Re: [Lua Problem] Weird table issue

Lee
Moderator Off Offline

Quote
That's actually expected, non-numerical indices are not shown in the subexpressions, so you can use see(factions[0]) to see the full fields.

If you try to index into factions[0].enemies, you will get an error seeing as that field doesn't exist. Since you've never changed the values of the faction table from all zeros, any time you have factions[faction[id]], it will evaluate to factions[0], and hence the above problem.

old Re: [Lua Problem] Weird table issue

EngiN33R
Moderator Off Offline

Quote
But I changed faction[2] to 4 and I was indexing factions[faction[2]].enemies, meaning it's factions[4].enemies. Either that, or I misunderstood you.

old Re: [Lua Problem] Weird table issue

EngiN33R
Moderator Off Offline

Quote
Derp. It doesn't because it somehow causes the problem.

See, what I'm doing is:

execute script

add bot with ID 2

lua faction[2]=4

kill bot with ID 2

receive error

That's why I'm asking because I can't understand what is the cause of this damn thing.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview