[closed] WML table navigation

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

Moderator: Forum Moderators

Post Reply
User avatar
ZombieKnight
Posts: 161
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

[closed] WML table navigation

Post by ZombieKnight »

Hi,
I'm using

Code: Select all

local u = wesnoth.units.get(wml.variables["unit.id"])
local u_table=u.__cfg
Whats the path to weapon (name=weapon_name), specials with index [index], specials id?
Last edited by ZombieKnight on March 31st, 2024, 12:29 pm, edited 1 time in total.
I had saurian in profile before, but I've merged my discord profile with forum one...
User avatar
Celtic_Minstrel
Developer
Posts: 2235
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: WML table navigation

Post by Celtic_Minstrel »

ZombieKnight wrote: March 18th, 2024, 4:01 pm

Code: Select all

local u = wesnoth.units.get(wml.variables["unit.id"])
local u_table=u.__cfg
First of all, that's completely pointless. It's roughly equivalent to:

Code: Select all

local u_table = wml.variables["unit"]
Although there's always the possibility that the stored unit has been changed without being unstored, in which case your code bypasses those changes.
ZombieKnight wrote: March 18th, 2024, 4:01 pm Whats the path to weapon (name=weapon_name), specials with index [index], specials id?
This is what I'd recommend for getting that information, assuming the stored unit hasn't been modified at all:

Code: Select all

local u = wesnoth.units.get(wml.variables["unit.id"])
local weapon = u.attacks[weapon_name]
local special_id = weapon.specials[index].id
If you prefer to work with the stored unit for whatever reason, it can still be done but is slightly more complicated:

Code: Select all

local u_table = wml.variables["unit"]
local weapon_table = wml.find_child(u_table, "attack", {name = weapon_name})
local specials = wml.get_child(weapon_table, "specials")
local specials_id = specials[index].contents.id
Please note that "index" in both cases counts starting from 1, not 0. Also, in 1.16, .contents probably needs to be replaced with [2].
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
ZombieKnight
Posts: 161
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: WML table navigation

Post by ZombieKnight »

Thanks,
I can't use first way, becouse changing weapons special id (and name) in u didn't changed it...
Yeah, I gues Ill try that child_range.
(I've already individually found that special in weapon using 3 for cycles)
I had saurian in profile before, but I've merged my discord profile with forum one...
Post Reply