Lua Error in World Conquest II

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Post Reply
woolymuffler
Posts: 6
Joined: February 26th, 2021, 12:51 am

Lua Error in World Conquest II

Post by woolymuffler »

Getting the following lua error, I believe when enemy units are supposed to drop an item (although I have seen item drops work, at least with 2 players):

Code: Select all

20210329 09:19:25 error scripting/lua: ~add-ons/World_Conquest_II/lua/items.lua:24: variable 'z_index' must be assigned before being used
stack traceback:
	[C]: in function '.error'
	lua/ilua.lua:131: in metamethod '__index'
	~add-ons/World_Conquest_II/lua/items.lua:24: in upvalue 'add_overlay'
	~add-ons/World_Conquest_II/lua/items.lua:104: in local 'cmd'
	lua/wml-utils.lua:145: in field 'handle_event_commands'
	lua/wml-flow.lua:246: in local 'cmd'
	lua/wml-utils.lua:145: in field 'handle_event_commands'
	lua/wml-flow.lua:6: in function <lua/wml-flow.lua:5>
Seems simple enough, it identifies the file and the problem (assuming I'm reading it correctly); z_index is being used without being defined. So I searched the entire add-on directory to confirm that z_index was never defined anywhere else, and it seems it's not (thus the error).

Unfortunately I'm having trouble figuring out what value to set z_index to in order to correct the issue; I haven't done lua before although I do have scripting experience in general; here is the function for reference:

Code: Select all

local function add_overlay(x, y, cfg)
	local items = scenario_items[x * 10000 + y]
	if not items then
		items = {}
		scenario_items[x * 10000 + y] = items
	end

	local z_order = cfg.z_order or 0
	wesnoth.add_tile_overlay(x, y, cfg)
	if (#items > 0) and ((items[#items].z_order or 0) > z_order) then
		local item_index = 0
		local did_insert = false
		for i, item in ipairs(items) do
			local other_z_index = item.z_index or 0
			if not did_insert and other_z_index > z_index then <----- this line is the issue
				item_index = i
				did_insert = true
			end
			wesnoth.remove_tile_overlay(x, y, item.name or item.image or item.halp)
			wesnoth.add_tile_overlay(x, y, item)
		end
		table.insert(items, item_index, {
			z_order = cfg.z_order,
			x = x, y = y,
			image = cfg.image,
			halo = cfg.halo,
			team_name = cfg.team_name,
			visible_in_fog = cfg.visible_in_fog,
			redraw = cfg.redraw,
			name = cfg.name
		})
	else
		table.insert(items, {
			z_order = cfg.z_order,
			x = x, y = y,
			image = cfg.image,
			halo = cfg.halo,
			team_name = cfg.team_name,
			visible_in_fog = cfg.visible_in_fog,
			redraw = cfg.redraw,
			name = cfg.name
		})
	end

end
I tried adding the line "local z_index = item.z_index" just above the line erroring, but that just made line 25 error instead of 24, complaining that it was being compared to nil.
woolymuffler
Posts: 6
Joined: February 26th, 2021, 12:51 am

Re: Lua Error in World Conquest II

Post by woolymuffler »

I may have gotten it fixed; I changed the problem line
from:
if not did_insert and other_z_index > z_index then

to:
if not did_insert and other_z_index > item.z_index then

My reasoning being the item.z_index definitely existed and the mod creator may have just had a typo. Since then I haven't gotten the error and I even got an item drop in single player; so far, so good.
woolymuffler
Posts: 6
Joined: February 26th, 2021, 12:51 am

Re: Lua Error in World Conquest II

Post by woolymuffler »

Spoke to soon, I had wondered why this fixed it (as it should have been exactly equivalent to setting z_index = item.z_index, and it was). Now getting the nil error instead, just like my last attempt to define z_index:

20210330 14:15:23 error scripting/lua: ~add-ons/World_Conquest_II/lua/items.lua:24: attempt to compare nil with number

So I'm back to original question, what value is z_index supposed to be set to? It's not item.z_index, unless maybe the z_index attribute for certain items is wrong? That would explain why it only pops sometimes, and there are item drops. Hmm, something to look for then.
vghetto
Posts: 410
Joined: November 2nd, 2019, 5:12 pm

Re: Lua Error in World Conquest II

Post by vghetto »

It might have meant to compare other_z_index with z_order, not z_index.
Also change item.halp to item.halo
woolymuffler
Posts: 6
Joined: February 26th, 2021, 12:51 am

Re: Lua Error in World Conquest II

Post by woolymuffler »

Thanks! I'll try those and see what I get.
woolymuffler
Posts: 6
Joined: February 26th, 2021, 12:51 am

Re: Lua Error in World Conquest II

Post by woolymuffler »

Good catch on the item.halp; it would have been the next error to trigger, even if the one before it stopped.

So far, I haven't gotten either lua error since changing z_index to z_order and fixing the typo of halp to halo. There have been multiple item drops by enemies as well; too soon to say for sure it's fixed, but it's looking good. Thanks again.
vghetto
Posts: 410
Joined: November 2nd, 2019, 5:12 pm

Re: Lua Error in World Conquest II

Post by vghetto »

I'm not sure if z_index is ever assigned. z_order does get assigned somewhere else in the code.
Probably all z_index should be renamed to z_order. I don't know.

Code: Select all

local other_z_index = item.z_order or 0
woolymuffler
Posts: 6
Joined: February 26th, 2021, 12:51 am

Re: Lua Error in World Conquest II

Post by woolymuffler »

It could be; but so far I haven't seen the lua error since making your suggested changes. If they start again, I'll look replacing z_index anywhere else it's called as well.
Post Reply