Get a table of all subtags

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

Moderators: Forum Moderators, Developers

Get a table of all subtags

Postby Pentarctagon » April 6th, 2017, 12:39 am

Is there a way to get a lua table representing all subtags? So a way to get:
Code: Select all
tab.option
{
  description="1",
   image="ball-blue.png",
   label="1.",
   tab.command
   {
     tab.set_variable
     {
         name="var",
         value=0
     }
   }
}


from:

Code: Select all
[custom tag]
  [option]
     description="1"
     image="ball-blue.png"
     label="1."
     [command]
        [set_variable]
           name="MusicSession.current_playlist"
           value=0
        [/set_variable]
    [/command]
  [/option]
[/custom_tag]
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Pentarctagon
Forum Administrator
 
Posts: 2972
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Get a table of all subtags

Postby gfgtdf » April 6th, 2017, 12:28 pm

I am not sure what exactly the differnece is beween your format and the normal lua format from for wml tables as returned by wesnoth.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.
gfgtdf
Developer
 
Posts: 912
Joined: February 10th, 2013, 2:25 pm

Re: Get a table of all subtags

Postby Pentarctagon » April 6th, 2017, 11:59 pm

I feel kind of stupid asking this, but how do I access the data then? With:
Code: Select all
local test = helper.get_variable_array("test")

wesnoth.message(tostring(test.description))
wesnoth.message(tostring(test[1].description))
wesnoth.message(tostring(test[1][1].description))
wesnoth.message(tostring(test[1].option[1].description))


the first 3 print nil, and the other prints an error about indexing a nil field.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Pentarctagon
Forum Administrator
 
Posts: 2972
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Get a table of all subtags

Postby mattsc » April 7th, 2017, 12:28 am

This does not directly answer your question, but hopefully it gives you the tools to do so. If you want to know the structure of any Lua table, I strongly recommend using debug_utils from the Wesnoth Lua Pack. debug_utils.dbms() is incredibly useful, IMO.
mattsc
 
Posts: 984
Joined: October 13th, 2010, 6:14 pm
Location: Wandering, mostly aimlessly

Re: Get a table of all subtags

Postby gfgtdf » April 7th, 2017, 12:48 am

but how do I access the data then? With:


what exxactly does your [test] wml array look like?
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.
gfgtdf
Developer
 
Posts: 912
Joined: February 10th, 2013, 2:25 pm

Re: Get a table of all subtags

Postby Pentarctagon » April 7th, 2017, 6:19 am

mattsc wrote:This does not directly answer your question, but hopefully it gives you the tools to do so. If you want to know the structure of any Lua table, I strongly recommend using debug_utils from the Wesnoth Lua Pack. debug_utils.dbms() is incredibly useful, IMO.


Alright, thanks, I'll take a look.

gfgtdf wrote:
but how do I access the data then? With:


what exxactly does your [test] wml array look like?


Code: Select all
[event]
  name="turn 1"
 
  [set_variables]
    name="test"
    [value]
     
      something="something"
      [option]
         description="1"
         image="ball-blue.png"
         label="1."
         [command]
            [set_variable]
               name="MusicSession.current_playlist"
               value=0
            [/set_variable]
        [/command]
      [/option]
     
    [/value]
  [/set_variables]
  [lua]
    code=<<
     
      helper = wesnoth.require "lua/helper.lua"
     
      local test = helper.get_variable_array("test")

      wesnoth.message(tostring(test[1].something))
     
    >>
  [/lua]
[/event]


"something" is correctly printed, at least.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Pentarctagon
Forum Administrator
 
Posts: 2972
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Get a table of all subtags

Postby mattsc » April 7th, 2017, 12:49 pm

If you run debug_utils.dbms() over that, you see that the table looks like this:
Code: Select all
{
    [1] = {
          [1] = {
                [1] = "option",
                [2] = {
                          [1] = {
                                [1] = "command",
                                [2] = {
                                      [1] = {
                                            [1] = "set_variable",
                                            [2] = {
                                                      value = 0,
                                                      name = "MusicSession.current_playlist"
                                                  }
                                            }
                                      }
                            },
                          label = "1.",
                          image = "ball-blue.png",
                          description = 1
                      }
                },
          something = "something"
          }
}

To extract, for example, the image file name:
Code: Select all
    local option = helper.get_child(test[1], 'option')
    wesnoth.message(option.image)
(You can put that into one line, too, of course.)
mattsc
 
Posts: 984
Joined: October 13th, 2010, 6:14 pm
Location: Wandering, mostly aimlessly

Re: Get a table of all subtags

Postby gfgtdf » April 7th, 2017, 12:52 pm

since [test] doesn't seem to be a wml array, its easier just to use wesnoth.get_variable directly instead of helper.get_variable_array wesnoth.get_varaible retruns a wml table as docuemnted in https://wiki.wesnoth.org/LuaWML#Encodin ... Lua_tables . You can use the helper functions helper.get_child and helper.child_range to acces subtags, for example as in
Code: Select all

  [lua]
    code=<<
     
      helper = wesnoth.require "lua/helper.lua"
     
      local test = wesnoth.get_variable("test")
      local option = helper.get_child(test, "option")

      wesnoth.message(tostring(option.description))
     
    >>
  [/lua]
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.
gfgtdf
Developer
 
Posts: 912
Joined: February 10th, 2013, 2:25 pm


Return to Lua Labs

Who is online

Users browsing this forum: No registered users and 0 guests