Dugi's lua questions
Moderator: Forum Moderators
Re: Dugi's lua questions
That should work assuming that variable.foo[3] exists and is a table. If not, you could do something like this (assuming variable.foo exists and is a table)Dugi wrote:I still need a reply to the question how can I add something="somethingelse" into the third variable array foo in a variable. variable.foo[3].something="somethingelse" like in WML does not seem to work
Code: Select all
if (not variable.foo[3]) then variable.foo[3] = {} end
variable.foo[3].something = "somethingelse"
Code: Select all
variable.foo[3] = { something = "somethingelse"}
Edit2: Actually, "equivalently" is wrong. They are only equivalent when variable.foo[3] does not exist or is empty. The first code snippet adds something = "somethingelse" to the table, while the second means that that is the entire table, deleting whatever might have previously existed in variable.foo[3] before..
Edit: if you're going to work with tables etc. a lot, I recommend downloading the Wesnoth Lua Pack add-on and getting familiar with the debug_utils.dbms() function.
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: Dugi's lua questions
Thanks a million, I'll try and see.
Still not working. I think that I am wrong somewhere. I exported the variable array into wml, and it created an array that looks like this:
Then I wanted to add a tooltip using the code to the list_definition.
I tried this (also some other variations, that didn't seem to help):
But it is telling that 'attempt to index field 'grid' (a nil value)'.
What am I doing wrongly? Any ideas?
Still not working. I think that I am wrong somewhere. I exported the variable array into wml, and it created an array that looks like this:
Spoiler:
I tried this (also some other variations, that didn't seem to help):
Code: Select all
dialog.grid.row[2].column.grid.row.column.grid.row[2].listbox.tooltip = "blablabla"
What am I doing wrongly? Any ideas?
Re: Dugi's lua questions
That error message means that the 'grid' field (one of the 3 grid fields) in that table doesn't exist yet (it has value 'nil'). So you need to create it (and potentially its subfields, depending on how you do it), for example by one of the methods in my earlier post.Dugi wrote:Then I wanted to add a tooltip using the code to the list_definition.
I tried this (also some other variations, that didn't seem to help):
Code: Select all
dialog.grid.row[2].column.grid.row.column.grid.row[2].listbox.tooltip = "blablabla"
But it is telling that 'attempt to index field 'grid' (a nil value)'.
What am I doing wrongly? Any ideas?
If you use "debug_utils.dbms(dialog)" from the WLP, it should tell you the structure of the table and you can see where things need to be added. (I don't have time to go through the WML table you posted right now, sorry.)
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: Dugi's lua questions
Unless you're referring directly to a WML variable, you can't use the same syntax to look into a WML table that you would use in WML. 'grid' is nil because multiple subtags can have the same name and are therefore indexed by number. Take the following example of a valid WML table:Dugi wrote: Then I wanted to add a tooltip using the code to the list_definition.
I tried this (also some other variations, that didn't seem to help):But it is telling that 'attempt to index field 'grid' (a nil value)'.Code: Select all
dialog.grid.row[2].column.grid.row.column.grid.row[2].listbox.tooltip = "blablabla"
What am I doing wrongly? Any ideas?
Code: Select all
dialog = {
-- Must be a string, number, boolean, or nil
grid = 42,
-- This is a subtag. Its index is 1.
T.grid{
-- Other attributes and subtags
}
}
Re: Dugi's lua questions
I have learned it recently. Thanks to mattsc's hint about that debug thing. I think I can live also with the usual way it is done.Unless you're referring directly to a WML variable, you can't use the same syntax to look into a WML table that you would use in WML.
EDIT: I have a suspicion that wesnoth.set_dialog_canvas crashes the game. If I changed the canvas to a table I used to open the dialog before (therefore, I replaced it with the same thing), and it crashed, this was the Terminal output:
Code: Select all
20121207 19:58:47 error gui/event: Invalid scrollbar mode 'no' falling back to 'initial_auto'.
20121207 19:58:47 error gui/parse: Canvas: found a shape of an invalid type tooltip.
wesnoth-1.10: /build/buildd/wesnoth-1.10-1.10.4/src/gui/auxiliary/canvas.cpp:1493: void gui2::tcanvas::parse_cfg(const config&): Assertion `false' failed.
Aborted (core dumped)
EDIT #3: Why is it impossible to set callbacks to sliders?
Re: Dugi's lua questions
Sorry for replying to myself, but nobody would notice this otherwise.
New question. If I use a lua-based wml tag containing a GUI and retrieval from persistent varriables in a synchronised event, shall it cause OoS errors?
New question. If I use a lua-based wml tag containing a GUI and retrieval from persistent varriables in a synchronised event, shall it cause OoS errors?
Re: Dugi's lua questions
Now seriously, could someone help me? I helped a lot of people with WML, so somebody could help me with lua till I learn it.
I am trying to store a unit type into a variable array, but this:
does not seem to work, although according to the wiki, it should be a variable array indexed by unit type ids. debug_utils.dbms tells that it contains only the unit's ID, nothing else, and it behaves like that. How can that thing in wml_tags.lua work, that is a mystery for me.
Any ideas how to store a unit type into a lua array?
This is what for example what wesnoth.unit_types["Necromancer"] contains:
with a metatable that cannot be read normally.
I am trying to store a unit type into a variable array, but this:
Code: Select all
local unit_type=wesnoth.unit_types["name of the desired unit"]
Any ideas how to store a unit type into a lua array?
This is what for example what wesnoth.unit_types["Necromancer"] contains:
Code: Select all
{
id="Necromancer"
}
-
- Inactive Developer
- Posts: 2461
- Joined: August 15th, 2008, 8:46 pm
- Location: Germany
Re: Dugi's lua questions
There are pretty few people in the forums who can answer lua questions. Developing is hard, and you are very well able to answer questions yourself. Many of the wml questions are just asked because people are being lazy, and there are many people who know enough wml, have enough time and are nice to help.Dugi wrote:Now seriously, could someone help me? I helped a lot of people with WML, so somebody could help me with lua till I learn it.
try like this (untested):
Code: Select all
local unit_type=wesnoth.unit_types.Troll.__cfg)
Then you can do quick trial & error and learn fast.
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml starters • Plan Your Advancements: mp mod
The Earth's Gut: sp campaign • Settlers of Wesnoth: mp scenario • Wesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
A Simple Campaign: campaign draft for wml starters • Plan Your Advancements: mp mod
The Earth's Gut: sp campaign • Settlers of Wesnoth: mp scenario • Wesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
Re: Dugi's lua questions
The Lua representations of unit types, units, and other things are proxy tables — rather than all the engine’s data on the Necromancer unit type being copied to a Lua table when you access
wesnoth.unit_types.Necromancer
, a proxy table is returned, which retrieves each attribute from the engine when you request it:Code: Select all
necro = wesnoth.unit_types.Necromancer -- necro is a proxy table.
necrohp = necro.hitpoints -- Retrieves the Necromancer’s hitpoints from the engine; necrohp is a normal number.
necrocfg = necro.__cfg -- Retrieves all the data immediately; necrocfg is a normal table.
- Elvish_Hunter
- Posts: 1576
- Joined: September 4th, 2009, 2:39 pm
- Location: Lintanir Forest...
Re: Dugi's lua questions
Sorry for the delay in answering, but real life got in the way. After all, we don't refuse to answer because we are (meant as people with Lua knowledge, and in my case also as developer) so heartless and like to see new coders suffering ; if we don't answer, usually it's simply because we don't have the time to do so.Dugi wrote:Now seriously, could someone help me? I helped a lot of people with WML, so somebody could help me with lua till I learn it.
You can't access other arrays inside a WML array in this way; you need to use the helper.child_range() function. It becomes a bit more clear if you think how the Lua table structure will be:Dugi wrote:Code: Select all
dialog.grid.row[2].column.grid.row.column.grid.row[2].listbox.tooltip = "blablabla"
Code: Select all
dialog = { { "grid", { "row", { "column", {}}, { "column" {}} } } }
About your tooltip and scroll_label issue: please note that GUI2/Lua almost certainly still contains some bugs, and if you find one of them you should at least inform mordante.
Does the GUI ask the user to input values in some way? If yes, then also the answer is yes, and you must use wesnoth.synchronize_choice() to return a WML table containing the selected values and avoid OOS.Dugi wrote:New question. If I use a lua-based wml tag containing a GUI and retrieval from persistent varriables in a synchronised event, shall it cause OoS errors?
Anonymissimus and 8680 pretty much answered to this; I want only to point two things:Dugi wrote:Any ideas how to store a unit type into a lua array?
1) the documentation of wesnoth.unit_types is at http://wiki.wesnoth.org/LuaWML:Units#wesnoth.unit_types . You can notice the __cfg field, but you can see it also in other functions (wesnoth.get_units, for example). What's its purpose? It returns a read_only WML table, containing all the same data that you can see in the inspect window. This means that, if you want to read a WML table inside of it, you'll have to use helper.child_range.
2) Lua does not have such thing as "array". Lua has tables, that are different from WML arrays as they can contain also unindexed values and functions. Example:
Code: Select all
local t = { "spam", "eggs", "bacon", 42 }
Code: Select all
[t]
spam
[/t]
[t]
eggs
[/t]
[t]
bacon
[/t]
[t]
42
[/t]
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)
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
Re: Dugi's lua questions
So, thank you Anonymissimus and 8680, thanks to you I should be able to continue with the thing I was doing.
@Elvish_Hunter I have learned the way to work with lua tables already, but you made it clearer to me anyway.
Who is mordante? Never seen a post from him.
How do I use wesnoth.synchronize_choice? According to wiki, it should wrap the GUI, will that cause that all variables created by its callbacks and post_show functions will be synchronised?
The following advice was also useful, thanks.
I would have written more, but my neck is too tired from a metal concert to keep my head in the position I need
@Elvish_Hunter I have learned the way to work with lua tables already, but you made it clearer to me anyway.
Who is mordante? Never seen a post from him.
How do I use wesnoth.synchronize_choice? According to wiki, it should wrap the GUI, will that cause that all variables created by its callbacks and post_show functions will be synchronised?
The following advice was also useful, thanks.
I would have written more, but my neck is too tired from a metal concert to keep my head in the position I need
Re: Dugi's lua questions
Another issue. When I create a unit, with some tables/variable arrays inside its [variables] (this works), I somehow cannot get these variables from there.
This is the table describing the unit (nothing strange with it; unimportant parts were removed):
I am trying to make something read the ids of the tables named 'a', I mean those 10301, 112, 218, 305, etc, but even unit[4] is said to be nil.
This is the table describing the unit (nothing strange with it; unimportant parts were removed):
Spoiler:
- Elvish_Hunter
- Posts: 1576
- Joined: September 4th, 2009, 2:39 pm
- Location: Lintanir Forest...
Re: Dugi's lua questions
mordante is our GUI expert. You can contact him on IRC, channel #wesnoth-dev.Dugi wrote:Who is mordante? Never seen a post from him.
How do I use wesnoth.synchronize_choice? According to wiki, it should wrap the GUI, will that cause that all variables created by its callbacks and post_show functions will be synchronised?
Some examples on how to use wesnoth.sychronize_choice are available in the WLP, file gui_tags.lua. Here there is one that I made some time ago:
Code: Select all
local function sync()
local function item_postshow()
-- here get all widget values
end
local return_value = wesnoth.show_dialog( item_dialog, item_preshow, item_postshow )
return { return_value = return_value }
end
local return_table = wesnoth.synchronize_choice(sync)
if return_table.return_value == 1 or return_table.return_value == -1 then
wesnoth.set_variable ( cfg.variable or "item_picked", "yes" )
else wesnoth.set_variable ( cfg.variable or "item_picked", "no" )
end
That's right, you can't. Only first level variables are proxied, this means that you can't directly access arrays inside [variables].Dugi wrote:When I create a unit, with some tables/variable arrays inside its [variables] (this works), I somehow cannot get these variables from there.
To do this, you can use again the .__cfg field. Let's assume that you already stored the unit with wesnoth.get_units(), and it's inside the u variable:
Code: Select all
local vars = helper.get_child( u.__cfg, "variables" )
Code: Select all
for item in helper.child_range( vars, "a" ) do
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)
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
Re: Dugi's lua questions
Damn, I should have remembered that. Thanks a lot.
It wasn't what I needed, it was not a metatable, it was a regular lua table.
It wasn't what I needed, it was not a metatable, it was a regular lua table.
Re: Dugi's lua questions
I believe
helper.get_child(u.__cfg, "variables")
can be replaced by u.variables.__cfg
, eliding the subtag search and minimizing the (slow) cfg dump.What wasn’t what you needed, and why did you need a metatable?Dugi wrote:It wasn't what I needed, it was not a metatable, it was a regular lua table.