[set_variables] doesn't seem to do anything

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

Moderator: Forum Moderators

Post Reply
Luther
Posts: 128
Joined: July 28th, 2007, 5:19 pm
Location: USA

[set_variables] doesn't seem to do anything

Post by Luther »

I'm looking for a way to feed raw WML files into Lua so I can extract data from them, but no matter what I do, I can't get [set_variables] to actually store any data. The following code is in a testing state to show exactly what doesn't work.

Here's my WML:

Code: Select all

  [event]
    name=prestart

    [set_variables]
      name=unit_type_data
      [value]
        [unit_type]
          id=Blood Bat
        [/unit_type]
        #{core/units/bats/Bat_Blood.cfg}
      [/value]
    [/set_variables]

    [tcg_init]
      size={SIZE}
      variable=unit_type_data
    [/tcg_init]
  [/event]
And here's my Lua:

Code: Select all

function W.tcg_init(cfg)
  local v = cfg.variable
  V.unitTypeList = ''
  for k, v in pairs(V[v]) do
    wesnoth.message(tostring(k))
    wesnoth.message(tostring(v))
  end
  for unitType in H.child_range(V[v], 'unit_type') do
    V.unitTypeList = ('%s%s,'):format(V.unitTypeList, unitType.id)
  end
  wesnoth.message(V.unitTypeList) --Empty
  --V[v] = nil

  V.recruitListSize = tonumber(cfg.size) or Map:biggestCastle()
  V.recruits = {}
  local i = 1
  while wesnoth.get_side(i) do
    V.recruits[i] = {}
    i = i + 1
  end
end
The first for loop above indicates that the variable consists only of {__varname = 'unit_type_data'}. I can't use wesnoth.get_unit_type_ids, because it needs to work on MP. I'm completely at a loss here...

EDIT: I'm using 1.8.5.
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: [set_variables] doesn't seem to do anything

Post by Anonymissimus »

You seem to be using the helper.set_metatables stuff which always confuses me so I don't quite understand - however, my guess is you are mistaken about what the variable is supposed to look like.
Here's the "syntactically correct" output from dbms (in wlp) for your unit_type_data variable (dbms(wesnoth.get_variable("unit_type_data"))):

Code: Select all

{
    [1] = {
                  [1] = "unit_type",
                  [2] = {
                                id = "Blood Bat"
                            }
              }
}
Good idea is also :inspect for doing this, but it however doesn't show the wml table structure.
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml startersPlan Your Advancements: mp mod
The Earth's Gut: sp campaignSettlers of Wesnoth: mp scenarioWesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
Luther
Posts: 128
Joined: July 28th, 2007, 5:19 pm
Location: USA

Re: [set_variables] doesn't seem to do anything

Post by Luther »

Anonymissimus wrote:You seem to be using the helper.set_metatables stuff which always confuses me so I don't quite understand
Supposedly, 'V[v]' should return the same thing as 'wesnoth.get_variable(v)', but that doesn't seem to be the case for tables (see below). BTW, I couldn't find dbms in wlp. Is it only in the 1.9 version?
Good idea is also :inspect for doing this, but it however doesn't show the wml table structure.
Thanks, I didn't know about that command. It was very useful here.

I think I know enough to get it working now. Here's my current code with all the debugging statements:

WML:

Code: Select all

  [event]
    name=prestart

    [set_variables]
      name=unit_type_data
      [value]
        {core/units/bats}
      [/value]
    [/set_variables]

    [tcg_init]
      size={SIZE}
      variable=unit_type_data
    [/tcg_init]
  [/event]
Lua:

Code: Select all

function W.tcg_init(cfg)
  local v = cfg.variable
  V.unitTypeList = ''
  local data = wesnoth.get_variable(v)

  --Shows indexes 1,2,3, but no __varname
  for k, v in pairs(data) do
    wesnoth.message(tostring(k))
    wesnoth.message(tostring(v))
  end

  --Shows only the __varname index
  for k, v in pairs(V[v]) do
    wesnoth.message(tostring(k))
    wesnoth.message(tostring(v))
  end

  --Doesn't do anything
  for unitType in H.child_range(V[v], 'unit_type') do
    V.unitTypeList = ('%s%s,'):format(V.unitTypeList, unitType.id)
  end
  wesnoth.message(V.unitTypeList) --Empty

  --Commented out for game save inspection
  --V[v] = nil

  V.recruitListSize = tonumber(cfg.size) or Map:biggestCastle()
  V.recruits = {}
  local i = 1
  while wesnoth.get_side(i) do
    V.recruits[i] = {}
    i = i + 1
  end
end
Apparently, when V[v] returns a table, it actually returns a nearly empty table with a metatable that lets you make changes to the WML object. However, since the table is empty, it's impossible to traverse it with a for loop, which will force me to fall back on wesnoth.get_variable.

I don't have time to work on it right now, but thanks for your help. I'll let you know later if it still doesn't help.
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: [set_variables] doesn't seem to do anything

Post by Elvish_Hunter »

Luther wrote:BTW, I couldn't find dbms in wlp. Is it only in the 1.9 version?
It is only in the version available on Wesnoth-UMC-Dev, trunk branch, for now.
Current maintainer of these add-ons, all on 1.16:
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: [set_variables] doesn't seem to do anything

Post by Anonymissimus »

My dbms function is here:
https://wesnoth-umc-dev.svn.sourceforge ... _utils.lua
I wrote it since I was always confused about tables, specifically about why certain tables aren't also wml tables.
Interesting thing is that your {core/units/bats/Bat_Blood.cfg} inclusion works here...loading arbitrary wml syntax-like data into lua tables...
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml startersPlan Your Advancements: mp mod
The Earth's Gut: sp campaignSettlers of Wesnoth: mp scenarioWesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
Post Reply