Converting a unit to a lua table?
Moderator: Forum Moderators
Converting a unit to a lua table?
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.
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.
Re: Converting a unit to a lua table?
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.
Re: Converting a unit to a lua table?
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.Re: Converting a unit to a lua table?
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.Dugi wrote:To store a unit, use simplyu = 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 likeu = u.__cfg
. You can join the two together asu = wesnoth.get_units( filter )[1].__cfg
. I have no experience with translatable fields.
Re: Converting a unit to a lua table?
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
Re: Converting a unit to a lua table?
what serialisation method are you using, and what exactly is the problem? (the translatable strings maybe)CIB wrote:However, that one suffers the same problem, can't be serialized using a standard lua table serialization library.
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.
Re: Converting a unit to a lua table?
Not so easily actually, see: http://forums.wesnoth.org/viewtopic.php?f=58&t=39036gfgtdf wrote: you always can easily write your owm serialisation method, or extend a given serialisation method as lua is normaly 'open source'.
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.
Re: Converting a unit to a lua table?
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.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.
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;
}
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.