Converting a unit to a lua table?

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

Moderator: Forum Moderators

Post Reply
CIB
Code Contributor
Posts: 625
Joined: November 24th, 2006, 11:26 pm

Converting a unit to a lua table?

Post by CIB »

I'm working on a campaign written almost entirely in lua, and so having data available as pure lua tables is very useful. However, they should be pure lua tables, not userdata, since I also use lua table serialization for my "savegames".

It seems that wesnoth.copy_unit might do this, but I'm not entirely sure what "private unit" refers to. The other option seems to be to use wesnoth.fire to fire a store_unit action, store the unit in a temporary WML variable, then use wesnoth.get_variable to convert the WML sub-tree to a lua table.
CIB
Code Contributor
Posts: 625
Joined: November 24th, 2006, 11:26 pm

Re: Converting a unit to a lua table?

Post by CIB »

Hm, with the store_unit method I'm still running into the problem of it trying to store translatable text which seems to be a userdata rather than a lua table that can be simply translated back and forth with get_variable/set_variable. Meh. Might have to use a huge list of units and store units as indices into it after all.
User avatar
Dugi
Posts: 4961
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Converting a unit to a lua table?

Post by Dugi »

To store a unit, use simply u = wesnoth.get_units( filter )[1] (saves the first unit that matched the filter). To convert it into a proper lua table, call its __cfg field, something like u = u.__cfg. You can join the two together as u = wesnoth.get_units( filter )[1].__cfg. I have no experience with translatable fields.
CIB
Code Contributor
Posts: 625
Joined: November 24th, 2006, 11:26 pm

Re: Converting a unit to a lua table?

Post by CIB »

Dugi wrote:To store a unit, use simply u = wesnoth.get_units( filter )[1] (saves the first unit that matched the filter). To convert it into a proper lua table, call its __cfg field, something like u = u.__cfg. You can join the two together as u = wesnoth.get_units( filter )[1].__cfg. I have no experience with translatable fields.
Oh thanks, I totally forgot about the __cfg field. However, that one suffers the same problem, can't be serialized using a standard lua table serialization library. Meh, I might just end up using player 8 recall list or something.
CIB
Code Contributor
Posts: 625
Joined: November 24th, 2006, 11:26 pm

Re: Converting a unit to a lua table?

Post by CIB »

Well, here's my solution now. The downside is that I now need to make sure my recall slave side's recall list is cleared whenever the side is actually used in combat.

Code: Select all

local recall_slave_side = 3
function helper.create_stored_unit(unit_table)
	local lastIndex = savegame.lastUnitIndex or 0
	unit_table.id = "sbu_"..tostring(lastIndex)
	unit_table.side = recall_slave_side
	local unit = wesnoth.create_unit(unit_table)
	wesnoth.put_recall_unit(unit, recall_slave_side)
	savegame.lastUnitIndex = lastIndex + 1
	return unit.id
end

function helper.unstore_unit(id, x, y, side)
	local unit = wesnoth.get_recall_units({ id = id })[1]
	unit.side = side
	wesnoth.put_unit(x, y, unit)
end

function helper.store_unit(unit)
	wesnoth.put_recall_unit(unit, recall_slave_side)
end
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: Converting a unit to a lua table?

Post by gfgtdf »

CIB wrote:However, that one suffers the same problem, can't be serialized using a standard lua table serialization library.
what serialisation method are you using, and what exactly is the problem? (the translatable strings maybe)
you always can easily write your owm serialisation method, or extend a given serialisation method as lua is normaly 'open source'.

plus you can also easily store units in wml variables with set_variable/get_variable
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
CIB
Code Contributor
Posts: 625
Joined: November 24th, 2006, 11:26 pm

Re: Converting a unit to a lua table?

Post by CIB »

gfgtdf wrote: you always can easily write your owm serialisation method, or extend a given serialisation method as lua is normaly 'open source'.
Not so easily actually, see: http://forums.wesnoth.org/viewtopic.php?f=58&t=39036

From that thread I gather it's impossible to get the untranslated string from lua, and saving the translated string to a savefile would be kinda silly.
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: Converting a unit to a lua table?

Post by gfgtdf »

CIB wrote: Not so easily actually, see: http://forums.wesnoth.org/viewtopic.php?f=58&t=39036

From that thread I gather it's impossible to get the untranslated string from lua, and saving the translated string to a savefile would be kinda silly.
my own thread ^^. Well, i think i can add a way to get untranslated strings to the core, but is isn't that trivial because translatable strings may be concated of serveral parts from different textdomains.


here how i'd do it (untested)

Code: Select all


static int intf_get_string_data(lua_State *L)
{
	t_string text = luaW_checktstring(L, 1);
	config retv;
	for (t_string::walker w(text); !w.eos(); w.next())
	{
		config& part = retv.add_child("part");
		std::string raw(w.begin(), w.end());
		part["value"] = raw;
		if(w.translatable())
		{
			part["domain"] = w.textdomain();
		}
	}
	luaW_pushconfig(L, retv);
	return 1;
}
EDIT: note, that i take not answering as "i think we don't need this feature".
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
Post Reply