Get a table of all subtags
Moderator: Forum Moderators
- Pentarctagon
- Project Manager
- Posts: 5584
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Get a table of all subtags
Is there a way to get a lua table representing all subtags? So a way to get:
from:
Code: Select all
tab.option
{
description="1",
image="ball-blue.png",
label="1.",
tab.command
{
tab.set_variable
{
name="var",
value=0
}
}
}
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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: Get a table of all subtags
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.
- Pentarctagon
- Project Manager
- Posts: 5584
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: Get a table of all subtags
I feel kind of stupid asking this, but how do I access the data then? With:
the first 3 print nil, and the other prints an error about indexing a nil field.
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))
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: Get a table of all subtags
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.
SP campaigns: Galuldur's First Journey (1.12 & 1.14) & Grnk the Mighty (1.10 & 1.12)
AI experiments: Micro AIs (wiki, forum thread, known/fixed bugs), Fred, AI-demos add-on
AI experiments: Micro AIs (wiki, forum thread, known/fixed bugs), Fred, AI-demos add-on
Re: Get a table of all subtags
what exxactly does your [test] wml array look like?but how do I access the data then? With:
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.
- Pentarctagon
- Project Manager
- Posts: 5584
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: Get a table of all subtags
Alright, thanks, I'll take a look.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.
gfgtdf wrote:what exxactly does your [test] wml array look like?but how do I access the data then? With:
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]
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: Get a table of all subtags
If you run debug_utils.dbms() over that, you see that the table looks like this:
To extract, for example, the image file name:
(You can put that into one line, too, of course.)
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"
}
}
Code: Select all
local option = helper.get_child(test[1], 'option')
wesnoth.message(option.image)
SP campaigns: Galuldur's First Journey (1.12 & 1.14) & Grnk the Mighty (1.10 & 1.12)
AI experiments: Micro AIs (wiki, forum thread, known/fixed bugs), Fred, AI-demos add-on
AI experiments: Micro AIs (wiki, forum thread, known/fixed bugs), Fred, AI-demos add-on
Re: Get a table of all subtags
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.