enclave's Lua thread

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

Moderator: Forum Moderators

Post Reply
User avatar
iceiceice
Posts: 1056
Joined: August 23rd, 2013, 2:10 am

Re: enclave's Lua thread

Post by iceiceice »

enclave wrote:thx iceiceice I always considered arrays starting with 0 pain in the a.. I dont remember a single case when array[0] would be more useful than array[1], rather was always problematic and required some extra code to make it work the way you want.. adding to the code i-1 , i+1
Yeah, it's even more fun in the C++ code that interacts between the WML and the lua and makes all the stuff actually happen... Besides this one there is also the one where, map locations are represented in the game as having coordinates one less than what you see on the screen OR in the WML, so 1,1 is actually 0,0 in the C++ code, but NOT in the lua code or the WML code, and you must add 1,1 whenever you display to user, or display any debugging output or you get hella confused :doh:
enclave wrote: wml is very nice regarding that you dont need to declare variables
Yeah so, maybe counterintuitively, I think this is not a good thing to help new programmers. Declaring variables in an interpretted language is not for speed, it is done to help the programmer. If wesnoth knows what names are supposed to be variables, it can tell you when you make a typo. If you don't declare variables... wesnoth can't figure it out automatically, you just see weird bugs in your add-on... and you die a slow death by errors...

In wesnoth 1.13, lua runs in "strict mode" which means "variables must be declared (by assigning) before their value is taken". 8)

http://forums.wesnoth.org/viewtopic.php?f=2&t=41481
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

true..

by the way OOS happen in turn 2 also..

when this code runs:
Spoiler:
Maybe I need to put local T.option somewhere.. or anything similar? Im so noob in it, may do basic mistakes anywhere..

in wesnoth 1.12 it gives following error explanation (doesnt even offer popup message with options), as in image attached:
Attachments
error on start of 1.12.1
error on start of 1.12.1
User avatar
iceiceice
Posts: 1056
Joined: August 23rd, 2013, 2:10 am

Re: enclave's Lua thread

Post by iceiceice »

Ah I see. It means you didn't set up the "T" object: LuaWML:Misc#helper.set_wml_tag_metatable

You need to include a line like

Code: Select all

T = helper.set_wml_tag_metatable {}
at the beginning of the preload event for the T object to exist. Otherwise it will just be nil. (If you aren't loading "helper" then you need to do that also.)
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

is it these lines i need to include?

Code: Select all

helper = wesnoth.require "lua/helper.lua"
T = helper.set_wml_tag_metatable {}
if yes then where is the best place to put them? every time in beginning of any [lua] <<
or only once on preload event or somehwere else?

Thanks iceiceice..

If it solve my problem I still dont understand why it worked flawlessly on local computer.. but oos appear in multiplayer..?

--------------------------------------------------------------------
PS. indeed, included this

Code: Select all

helper = wesnoth.require "lua/helper.lua"
T = helper.set_wml_tag_metatable {}
in preload event. Problem solved..
Before applying fix tried same code in wml:
Spoiler:
also no OOS.

So officially, OOS were caused not by [message] [option] on turn 1, but by not having these 2 lines in preload event:

Code: Select all

helper = wesnoth.require "lua/helper.lua"
T = helper.set_wml_tag_metatable {}
I even removed synchronized choice, works perfect without it.

Thank you iceiceice so much!!!!!!
Last edited by enclave on April 1st, 2015, 1:12 am, edited 2 times in total.
User avatar
iceiceice
Posts: 1056
Joined: August 23rd, 2013, 2:10 am

Re: enclave's Lua thread

Post by iceiceice »

The best is only in a preload event. Setting up helper, and these metatables, is somewhat the purpose of preload events. (Since, lua variables are not saved between sessions! :eng: So the library set up routines must happen at least once at the start before any game load.)
enclave wrote:If it solve my problem I still dont understand why it worked flawlessly on local computer.. but oos appear in multiplayer..?
So, that phenomenon in general can happen quite easily. It's easy to write WML that looks fine in local, but if you play in multiplayer, its broken. Or if you look at replay file, its broken. That's the nature of OOS problems. If you want for replays and mp to work, its best to test in mp, not just in local computer.

It's a bit odd that if you had a lua syntax error, that it wouldn't report that in local player also. When you use synchronize choice in local player, its simpler because there's no one to synchronize with. So an OOS won't be reported immediately, it will only occur when you watch the replay. So if for some reason that code just wasn't running at all, then there shouldn't be an error reported I guess.

In either version, I would expect that you would get big lua error reported on screen in local play also. In 1.10, it sounds like for some reason it was silently ignoring that T was nil, if that code was running. I'm not sure why that would happen, would have to see the whole add-on to test and figure it out. You could try peppering the lua block with "print('foo')" statements and what the type of T is, and check what happens in the console when you play local.

Also sometimes the game will suppress errors I think if you are not running in debug mode...? Not entirely sure actually.

It's usually a good idea to test things in debug mode and also not in debug mode. (:debug)
If you aren't in debug mode, I guess in some cases we might assume that its not the developer, its the players and they want to try to play through the bug no matter what so we don't show them errors. It really depends on opinions of the C++ developer who wrote what code it is :lol:
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Thank you very much iceiceice! You the life saver! and now I need to watch out for every lua I make.. after this experience :)
Already considering to change some code into wml :D joking.. will try not to..

im not running in debug mode, i think so.. I'm quite noob in some of the things.. I tried to use something like that, but even failed to turn it on :))))

/me stupid
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Hello again,
New questions with Lua. All regarding dialog T.things..

I'm making add-on called "Image Create" which is alternative, similar to "Image loading tester" by Dugi.
Trying to make it look nicer and user friendlier.

Please I would appreciate even if you answer any of the following questions in random order.. No need to answer them all straight away, it may take a lot of time. Please feel free to answer partly. I would be happy! The quicker I know the answers the better :)

1) Anyone knows if there is way to use callback for slider? As far as I understand the only use for slider is to set the value, but then you need to press some button somewhere to confirm the change..
2) if question #1 "callback" is possible, then is there a way to make vertical slider? as far as I understand we only have horizontal sliders available.. am i wrong..?
3) can T.buttons be resized to smaller size?
4) do you know the way to draw canvas with transparent fill but with lets say red border color..? so part of image behind it would be visible in red frame? (wesnoth.set_dialog_canvas(1, { T.rectangle { ...) I think fill_color = "0,0,0,255" didnt help. not 100% sure.
5) not relevant anymore.. found workaround.
Spoiler:
6) What is the postshow() for? I don't use it and don't really understand what difference it makes? On pressing enter key, always only happens the preshow() as far as I understand.. (res = wesnoth.show_dialog(image_create_dialog, preshow, postshow))
7) can radiobuttons be aligned verticaly? (T.horizontal_listbox , is there vertical version? I have not tried, because I guessed there isnt..)
9) radiobuttons have callback i hope? going to try soon..

PS. tried #7, changed to T.vertical_listbox, said wesnoth needs to close :) never even seen such error before with wrong lua codes.

Thank you very much!
Last edited by enclave on April 29th, 2015, 12:27 am, edited 3 times in total.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

nobody knows? :(
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: enclave's Lua thread

Post by Elvish_Hunter »

enclave wrote:nobody knows? :(
To be honest, there are only a few people around that coded GUI/Lua dialogs (right now, I can think of myself and vultraz). And most of the GUI stuff isn't well documented, so answers may even require some time to experiment.
That said...
enclave wrote:1) Anyone knows if there is way to use callback for slider? As far as I understand the only use for slider is to set the value, but then you need to press some button somewhere to confirm the change..
No, it isn't possible. Attempting to attach a callback to a slider leads to this message:

Code: Select all

bad argument #2 to 'set_dialog_callback' (unsupported widget)
enclave wrote:2) if question #1 "callback" is possible, then is there a way to make vertical slider? as far as I understand we only have horizontal sliders available.. am i wrong..?
You're right, we don't have vertical sliders.
enclave wrote:6) What is the postshow() for? I don't use it and don't really understand what difference it makes? On pressing enter key, always only happens the preshow() as far as I understand.. (res = wesnoth.show_dialog(image_create_dialog, preshow, postshow))
postshow() is used to catch the return value of the dialog: in fact, the Enter key returns a value of -1, the Esc key returns a value of -2 and every on screen button can return an arbitrary value. Once that you check the return value, you can read the value of all the other widgets and put them in a WML table, because the postshow function needs to be called by wesnoth.synchronize_choice() to avoid OOS errors. There are some examples in the Wesnoth Lua Pack.
enclave wrote:7) can radiobuttons be aligned verticaly? (T.horizontal_listbox , is there vertical version? I have not tried, because I guessed there isnt..)
PS. tried #7, changed to T.vertical_listbox, said wesnoth needs to close :) never even seen such error before with wrong lua codes.
Because vertical listboxes are simply called "listbox". You'll have to call them with T.listbox :)
enclave wrote:9) radiobuttons have callback i hope? going to try soon..
You can attach callbacks to both the toggle buttons and the listboxes containing them (our radiobuttons are simulated in that way). Protip: if you put a definition = "radio" inside a T.toggle_button, it'll look like a proper radiobutton.
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)
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Thank you so much Elvish_Hunter, you are always very helpful! Helped a lot!

Since its all poor documented I will try to answer my own questions when I get some time and after some experiments.. So others could learn something if they look for similar information.

You mentioned "every on screen button can return an arbitrary value" does it mean I can assign a button to slider somehow?
Where could I find any example how to assign a value to any button and then to use it on keypress? Particularilly interested in arrow keys, left right, up, down.. but any key would do.. :)
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: enclave's Lua thread

Post by Elvish_Hunter »

enclave wrote:You mentioned "every on screen button can return an arbitrary value" does it mean I can assign a button to slider somehow?
Not exactly. I mean that every button (like OK, Cancel, or everything else) can return a different code. The wiki says:
if the dialog was dismissed by a button click, the integer value associated to the button via the "return_value" keyword.
if the dialog was closed with the enter key, -1.
if the dialog was closed with the escape key, -2.
So, when creating a T.button, you'll need to add a return_value key. Example taken from the WLP:

Code: Select all

		local buttonbox = T.grid {
					T.row {
						T.column {
							T.button {
								label = _"OK",
								return_value = 1
							}
						},
						T.column {
							T.spacer {
								width = 10
							}
						},
						T.column {
							T.button {
								label = _"Cancel",
								return_value = 2
							}
						}
					}
				}
enclave wrote:Where could I find any example how to assign a value to any button and then to use it on keypress? Particularilly interested in arrow keys, left right, up, down.. but any key would do..
Currently there's no way (or at least, not that I'm aware of) to bind a keyboard key to a peculiar return value or to a callback. You can only attach return values to pushbuttons, taking in account that -1 is associated with the Enter key, and -2 with the Esc key.
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)
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Thank you Elvish_Hunter! I will soon experiment with it a bit.. will write here if anything interesting :)
User avatar
doofus-01
Art Director
Posts: 4122
Joined: January 6th, 2008, 9:27 pm
Location: USA

Re: enclave's Lua thread

Post by doofus-01 »

Sorry for being a bit off topic, but...
Elvish_Hunter wrote:Protip: if you put a definition = "radio" inside a T.toggle_button, it'll look like a proper radiobutton.
Where do you find such things? I thought maybe widget "definitions" like that were defined somewhere in /src/gui but I can't find them. Of course, I don't know C++, but I hoped I could at least find "radio".
BfW 1.12 supported, but active development only for BfW 1.13/1.14: Bad Moon Rising | Trinity | Archaic Era |
| Abandoned: Tales of the Setting Sun
GitHub link for these projects
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Post by gfgtdf »

doofus-01 wrote: I thought maybe widget "definitions" like that were defined somewhere in /src/gui but I can't find them. Of course, I don't know C++, but I hoped I could at least find "radio".
Thats becasue those definitions are defined in wml.
doofus-01 wrote: Where do you find such things?
You can check wesnoths core dialogs, most of them also use gui2.
If you find a something in a wesnoth dialog hat you'd like to use you can look in data/gui/default/window/ to find the corresponding dialog description.

radio buttons can for example be found in the options dialog in the addon-donwload window. The file is https://github.com/wesnoth/wesnoth/blob ... ptions.cfg
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.
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: enclave's Lua thread

Post by Elvish_Hunter »

doofus-01 wrote:Where do you find such things?
Exactly as gfgtdf said, I found it by reading the content of every cfg file in data/gui/default/window/. I ended up making a small text file (very confused, but nobody else will read it anyway :P ) in which I collected every definition that I could find there.
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