Elvish_Hunter's Lua thread

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

Moderator: Forum Moderators

Post Reply
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: Elvish_Hunter's Lua thread

Post by Elvish_Hunter »

After some time of experiments (and after working a lot on Python + Tkinter + ttk :eng: ), I managed to create a working dialog with Lua. It contains some widgets (a slider, a text box, a toggle button and a button), to which I have to set some starting values, and from which I have to retrieve the resulting values. You can see the result below.
The resulting dialog
The resulting dialog
Lua Dialog (copia).png (73.49 KiB) Viewed 5485 times
The problem that I have is that it seems like I can't set a starting value for slider and text_box (I know that I can use value= for slider and label= for text_box, but I need to have the code set them in the preshow function depending, for example, on a unit's movement points or name), and that I can't retrieve the value from slider in the postshow function (I gain an "unsupported widget" error). Finally, when I accidentally placed a non-existent widget (I used T.textbox instead of T.text_box), I gained a crash and an assertion failed error in the terminal:

Code: Select all

[textbox]
	history = other_recruits
	label = None
[/textbox]
src/gui/auxiliary/window_builder.cpp:119 ASSSERTION FAILED: 0
Trace/breakpoint trap
I'm currently on 1.9.7+svn (49936M). Am I doing something wrong, or did I hitten three bugs in a row?
Code for dialog is here:
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)
Exasperation
Posts: 462
Joined: June 8th, 2006, 3:25 am

Re: Elvish_Hunter's Lua thread

Post by Exasperation »

I hunted down the definitions of set_dialog_value and get_dialog_value. For get_dialog_value, the following widgets are currently supported: listbox, multipage, toggle button, text box. For set_dialog_value, it specifically supports listbox, multipage, and toggle button, then has a default case which tries to handle the widget as a tcontrol (which apparently succeeds for a label but not a textbox or slider).
Looks like silene just didn't get around to adding support for any other widgets to those functions before leaving. :(
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: Elvish_Hunter's Lua thread

Post by Elvish_Hunter »

Thank you :) . I reported this as bug #18271 on Gna! https://gna.org/bugs/index.php?18271
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: Elvish_Hunter's Lua thread

Post by Anonymissimus »

Exasperation: You are ready to make a patch that adds the feature then ? :P I guess it's enough to add the according else if() {... } blocks in the same way as for the already supported widgets. Other than that I recognize the same, currently explicitely supported:

set_dialog_value:
listbox
multi_page
toggle_button

get_dialog_value:
listbox
multi_page
toggle_button
text_box

All other widgets may or may not work. So both aren't bugs, just lack of features. :P
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
Exasperation
Posts: 462
Joined: June 8th, 2006, 3:25 am

Re: Elvish_Hunter's Lua thread

Post by Exasperation »

We'll see. At the time I type this, I'm compiling an attempt at adding support for slider getting/setting. Assuming this works, I'll look at what other widgets might be feasible for me to do. It does look like a pretty simple task, there's a fairly obvious pattern to follow from the ones silene already did.

I think the other part of Elvish_Hunter's report is a bug, though. If you try to create a widget of a type that doesn't exist, it should handle the task more gracefully (I've seen devs say that if WML causes a crash, it's a bug; this doesn't exactly fall into that category, but it's close enough that I think the same logic applies).
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Elvish_Hunter's Lua thread

Post by Anonymissimus »

I found out why some widgets work and others don't - the ones derived from the class tcontrol do, the others don't since the cast fails then. You should ask mordante what would be the most suitable type(s) to cast to so that a maximum of widgets are supported.

The assertion failure is plainly annoying of course (I've come used to the scenario staying open no matter what I do, and there's just a lua error...).
Silene wrote about how to solve these here:
http://forums.wesnoth.org/viewtopic.php ... 34#p453534
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
Exasperation
Posts: 462
Joined: June 8th, 2006, 3:25 am

Re: Elvish_Hunter's Lua thread

Post by Exasperation »

Well, tslider and ttext_box are both indirectly derived from tcontrol; that's why trying to set the value of a slider fails silently instead of giving an error (like trying to get the value of a slider does). But they need to be handled differently than a "typical" tcontrol. For the tcontrol default case, you use the input as a string and call the set_label() member function. For a slider, you need to use the input as a number, check to make sure it's in bounds, then call the set_value() member function. For a text_box, it looks like you need to treat the input as a string but use set_value() (which, unlike the slider, comes from being derived from ttext_) instead of set_label(). I'm not sure what other widgets have distinct values that you might need to get/set. Maybe tprogress_bar (set_percentage()/get_percentage)?

In fact, it looks like all the individual widgets are derived from tcontrol, directly or indirectly. The issue just seems to be whether "value" is synonymous with "label" for that particular widget.
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: Elvish_Hunter's Lua thread

Post by Elvish_Hunter »

Thank you, now slider and text_box work fine. :D I needed them for a project, and below there are the details.
As a UMC author/maintainer, several times I used the debug console. And several times I did some typos, that enforced me to retype the command from scratch. I also noticed that the most used attributes that I modify in a unit while debugging are MP, XP, HP and attacks left; also, statuses cannot be modified from the debug console - they aren't a "first level variable". :twisted:
So, I made this tag, called [show_quick_debug]. It is meant to be used inside a [set_menu_item], and this is why it "stores" the unit at x1,y1. When used with a code like this

Code: Select all

		[set_menu_item]
		id=quick_debug
			description=Quick Debug
			[command]
				[show_quick_debug]
				[/show_quick_debug]
			[/command]
		[/set_menu_item]
if there is no unit in the hex shows a warning, but if there is a unit...
Quick Debug.jpg
It seems to work well, except for two problems:
- when modifying a unit's facing from Lua, it seems to not show immediatly, even after [redraw]
- setting a unit's side using Lua (es. side=3) causes a Segmentation Fault if that side is invalid because the scenario does not have enough sides (only two sides, for example). I worked around it, and such workaround will remain even if the crash will be fixed.
The parameters that can be modified are all those that don't require accessing the .__cfg field, and then using put_unit.
I'm also planning to add support for translatable strings, but the most important thing was making it up and running.
Any comments, suggestion, etc.? By the way, if nobody is against it, I'll put it in the WLP. :)
And here there are more than 250 lines of code:
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)
User avatar
Crendgrim
Moderator Emeritus
Posts: 1328
Joined: October 15th, 2010, 10:39 am
Location: Germany

Re: Elvish_Hunter's Lua thread

Post by Crendgrim »

If there is enough space, I would change the first two lines. By now, you have a layout like this:

Code: Select all

   X  14                Y  10                    ID  Elvish Archer-17      Valid  map
Type  Elvish Archer  Name  Urandeladir  Can recruit  no                Petrified  no
This looks a bit cluttered up and you'll search long for the desired information.
My suggestion would be to keep the labels in the top column and use the bottom column for the values:

Code: Select all

 X   Y   Type           Name         ID                Can recruit  Valid  Petrified
 14  10  Elvish Archer  Urandeladir  Elvish Archer-17  no           map    no
IMO, it's clearer this way. :?


Crend
UMC Story Images — Story images for your campaign!
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Elvish_Hunter's Lua thread

Post by Anonymissimus »

Great. I'm eager to try it.
Elvish_Hunter wrote:- setting a unit's side using Lua (es. side=3) causes a Segmentation Fault if that side is invalid because the scenario does not have enough sides (only two sides, for example).
fixed
Elvish_Hunter wrote:By the way, if nobody is against it, I'll put it in the WLP. :)
Fits perfectly into debug_utils.lua. Other than that I dont know whether it's better to have it as a function or as a tag.

For hitpoints and experience I suggest default values: hitpoints to 1 (checkboxes: 0 ? kill+fire_event ? ) and exp to max_exp (checkbox: advance).

EDIT
However - in the end it may make more sense if you could add such functionality to the C++ debug functionalities. Setting more than just toplevel attributes is a feature request since long, and the inspect dialog was apparently planned to have setting functionalities too.
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
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: Elvish_Hunter's Lua thread

Post by Elvish_Hunter »

Crendgrim wrote:If there is enough space, I would change the first two lines.
It is possible, but of course I'd like to hear some more opinions on that matter before modifying it (mainly because creating and modifying such a WML table, without having keys like row=, column=, rowspan= and columnspan= for every widget was quite hard).
Anonymissimus wrote:Great. I'm eager to try it.
Thanks!
Anonymissimus wrote:Fits perfectly into debug_utils.lua.
I was thinking about another solution: given that probably in future we'll see more graphical tags/functions, I thought of creating a gui-tags.lua file, linked to the wml-tags.lua by using wesnoth.require. What about it?
Anonymissimus wrote:Other than that I dont know whether it's better to have it as a function or as a tag.
We must consider that this dialog may be used also by UMC authors that don't know Lua, so I think that the tag form will be simpler for them - it could be put in a macro called in every scenario, for example.
Anonymissimus wrote:For hitpoints and experience I suggest default values: hitpoints to 1 (checkboxes: 0 ? kill+fire_event ? ) and exp to max_exp (checkbox: advance).
Default values are taken from the unit stats; in this peculiar case, the unit had no XP, hence the slider is set to 0. If it had 32 XP, the slider will be set at 32, for example. If you're referring to the slider limits, that probably may be done by creating the dialog table after having get_unit acquiring the unit, and before having show_dialog showing it. Not sure if it'll work.
EDIT: modification done, it works. I decided to place one more checkbutton, instead of the three suggested by you, called Fire events. If selected, the unit is killed when its HP are set to 0, and advances when its XP are set to maximum.
New code:
Anonymissimus wrote:However - in the end it may make more sense if you could add such functionality to the C++ debug functionalities. Setting more than just toplevel attributes is a feature request since long, and the inspect dialog was apparently planned to have setting functionalities too.
This is a matter for our C++ coders; and anyway, the inspect window has a lot of parameters, and having one smaller dialog with only the most used parameters is useful as well.
Since you talked about C++, can you suggest me any good tutorial, apart from the one at cplusplus.com ?
EDIT 2: I added support for translatable strings and [show_quick_debug] is on umc-dev.
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: Elvish_Hunter's Lua thread

Post by Anonymissimus »

Elvish_Hunter wrote: If selected, the unit is killed when its HP are set to 0, and advances when its XP are set to maximum.
What else could one want than the unit being killed when setting hitpoints to 0 ?
What else could one want than the unit being advanced when setting experience to max_experience ?
So maybe you should just make these the only conditions. Both are very frequent.
Also, the animations should be played when killing/advancing (testing unit animations).
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
Exasperation
Posts: 462
Joined: June 8th, 2006, 3:25 am

Re: Elvish_Hunter's Lua thread

Post by Exasperation »

Anonymissimus wrote:What else could one want than the unit being killed when setting hitpoints to 0 ?
What else could one want than the unit being advanced when setting experience to max_experience ?
So maybe you should just make these the only conditions. Both are very frequent.
Also, the animations should be played when killing/advancing (testing unit animations).
Elvish_Hunter: If you do decide to follow this advice, note that you can set special labels on a slider to be displayed instead of the max and/or min. So you could add something like

Code: Select all

minimum_value_label = "Kill Unit",
maximum_value_label = "Full Health"
to the hp slider and

Code: Select all

maximum_value_label = "Level Up"
to the exp slider.

Overall, this is looking great!
User avatar
melinath
Posts: 1298
Joined: May 20th, 2009, 7:42 am

Re: Elvish_Hunter's Lua thread

Post by melinath »

Thanks for the work on the widgets, Exasperation! Also, EH - looks great! Would it be all right with you if I port this into ModularLua?
Anonymissimus wrote: What else could one want than the unit being killed when setting hitpoints to 0 ?
I frequently want to set the hitpoints to 0 to ensure a kill when one unit attacks another.
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: Elvish_Hunter's Lua thread

Post by Elvish_Hunter »

Anonymissimus wrote:Great. I'm eager to try it.
Exasperation wrote:Overall, this is looking great!
melinath wrote:Also, EH - looks great!
Three great in a row. It must be a record. Thanks! :D
Anonymissimus wrote:What else could one want than the unit being killed when setting hitpoints to 0 ?What else could one want than the unit being advanced when setting experience to max_experience ?So maybe you should just make these the only conditions. Both are very frequent.
Done...
Anonymissimus wrote:Also, the animations should be played when killing/advancing (testing unit animations).
... done (although it required me to add a small wesnoth.delay to fire animations correctly)...
Exasperation wrote:note that you can set special labels on a slider to be displayed instead of the max and/or min.
... and done. Strangely, now I obtain this warning in the terminal:

Code: Select all

warning gui/draw: Text: text is too wide for the canvas and will be clipped.
Then everything works fine. :?
I also added translation marks, translations directory, and textdomain. Unfortunately, wmlxgettext doesn't seem able to handle Lua files. :annoyed: In case that you want to know, this was my command line and its output:

Code: Select all

linux@linux-desktop:~/wesnoth/data/tools$ '/home/linux/wesnoth/data/tools/wmlxgettext' '/home/linux/.local/share/wesnoth/1.9/data/add-ons/Wesnoth_Lua_Pack'
msgid ""
msgstr ""
"Project-Id-Version: wesnoth 1.9.7+svn\n"
"Report-Msgid-Bugs-To: http://bugs.wesnoth.org/\n"
"POT-Creation-Date: 2011-06-29, 21:22+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL\@ADDRESS>\n"
"Language-Team: LANGUAGE <LL\@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
I had to temporarily rename gui-tags.lua to to gui-tags_copia.cfg to obtain a good output (copia in Italian means copy; and yes, I know that I can add >file.po to redirect the output to a file)
Spoiler:
melinath wrote:Would it be all right with you if I port this into ModularLua?
It's open source, right? You can do whatever you want. :mrgreen: Perhaps wait some time (say, one week?) to see if any issue emerges?
melinath wrote:I frequently want to set the hitpoints to 0 to ensure a kill when one unit attacks another.
Well, restoring the Fire events checkbutton isn't too hard, if needed. It is safe on umc-dev.
Also, Anonymissimus, Exasperation and melinath: do you like the current upper labels layout, do you prefer Crendgrim's proposal, or do you have another suggestion?
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)
Post Reply