The best way to access unit variables subcontainers

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

Moderators: Forum Moderators, Developers

Post Reply
Posts: 7
Joined: September 23rd, 2016, 2:37 am

The best way to access unit variables subcontainers

Post by Pine » October 7th, 2016, 6:05 am

I'm trying to figure out a way to read and modify unit.variables.<container>.<attributes>. I noticed that lua interface has proxy support for unit.variables.<attribute> through wesnoth.get_unit api. Too bad that doesn't extend recursively to child containers providing easy access to grouped variables. Alternative methods to access same variable seems to be through unit.__cfg and then calling wesnoth.put_unit after state has been modified but this seems to be quite heavy operation for a simple variable modification.

Of course lua code can use a workaround where attribute names include containers with underscore separating container name from attribute name. That would allow using stable lua interface to access unit variables easily. Negative effect would be that attributes aren't grouped to containers in save game. The container separation would make reading state a bit easier when looking for broken state causing some bug.

Does anyone have a better alternative for unit variable sub-containers?


After a break think start to look a bit better. I now noticed that unit.variables.__cfg exists that allows modifications to variables fairly easily. Following is example how I'm thinking to implement lua access to unit.variables.

Code: Select all

local getInventory = function(unit)
	local cfg = unit.variables.__cfg
	local ns = bob.H.get_child(cfg, "bob")
	local inventory = bob.H.get_child(ns, "inventory")
	return cfg, inventory

local getItemCount = function(unit, id)
	local cfg, inventory = getInventory(unit)
	return inventory[id]

local setItemCount = function(unit, id, value)
	local cfg, inventory = getInventory(unit)

	inventory[id] = value
	unit.variables.__cfg = cfg
To confirm everyone guess. I'm implementing Bob's RPG era with lua to reduce its impact on save game size. That should hopefully speed up games when autosaves are enabled.

It would still be nice to have whole unit.variables as a proxy tree. Too bad proxy tree as it is now won't help much. iteration over sub-containers and attributes would be important feature. I don't know anything about lua interface towards engine if that is realistic to implement. But if it would be possible to iterate attributes with pairs and sub-containers with ipairs that would make proxy objects extremely useful for lua scripting.

Posts: 1190
Joined: February 10th, 2013, 2:25 pm

Re: The best way to access unit variables subcontainers

Post by gfgtdf » October 10th, 2016, 4:10 pm

This depends on whether you work on wesnoth 1.13 or wensot 1.12, in wesnoth 1.13.5 it ia afaik posible to use it like unit.variables["bob.inventory. " .. id] = value

EDIT: also note that you cannot use arbitary strings as wml tagnames, You need to mkae sure that id is of abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789_
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