enclave's Lua thread
Moderator: Forum Moderators
Re: enclave's Lua thread
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 confusedenclave 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 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...enclave wrote: wml is very nice regarding that you dont need to declare variables
In wesnoth 1.13, lua runs in "strict mode" which means "variables must be declared (by assigning) before their value is taken".
http://forums.wesnoth.org/viewtopic.php?f=2&t=41481
Re: enclave's Lua thread
true..
by the way OOS happen in turn 2 also..
when this code runs:
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:
by the way OOS happen in turn 2 also..
when this code runs:
Spoiler:
in wesnoth 1.12 it gives following error explanation (doesnt even offer popup message with options), as in image attached:
Re: enclave's Lua thread
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
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.)
You need to include a line like
Code: Select all
T = helper.set_wml_tag_metatable {}
Re: enclave's Lua thread
is it these lines i need to include?
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
in preload event. Problem solved..
Before applying fix tried same code in wml:
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:
I even removed synchronized choice, works perfect without it.
Thank you iceiceice so much!!!!!!
Code: Select all
helper = wesnoth.require "lua/helper.lua"
T = helper.set_wml_tag_metatable {}
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 {}
Before applying fix tried same code in wml:
Spoiler:
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 {}
Thank you iceiceice so much!!!!!!
Last edited by enclave on April 1st, 2015, 1:12 am, edited 2 times in total.
Re: enclave's Lua thread
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! So the library set up routines must happen at least once at the start before any game load.)
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
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.enclave wrote:If it solve my problem I still dont understand why it worked flawlessly on local computer.. but oos appear in multiplayer..?
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
Re: enclave's Lua thread
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 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
Already considering to change some code into wml 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
Re: enclave's Lua thread
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.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!
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:
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.
- Elvish_Hunter
- Posts: 1575
- Joined: September 4th, 2009, 2:39 pm
- Location: Lintanir Forest...
Re: enclave's Lua thread
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.enclave wrote:nobody knows?
That said...
No, it isn't possible. Attempting to attach a callback to a slider leads to this message: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..
Code: Select all
bad argument #2 to 'set_dialog_callback' (unsupported widget)
You're right, we don't have vertical sliders.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..?
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: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))
Because vertical listboxes are simply called "listbox". You'll have to call them with T.listboxenclave 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.
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 aenclave wrote:9) radiobuttons have callback i hope? going to try soon..
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)
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
Re: enclave's Lua thread
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..
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..
- Elvish_Hunter
- Posts: 1575
- Joined: September 4th, 2009, 2:39 pm
- Location: Lintanir Forest...
Re: enclave's Lua thread
Not exactly. I mean that every button (like OK, Cancel, or everything else) can return a different code. The wiki says:enclave wrote:You mentioned "every on screen button can return an arbitrary value" does it mean I can assign a button to slider somehow?
So, when creating a T.button, you'll need to add aif 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.
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
}
}
}
}
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.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..
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: enclave's Lua thread
Thank you Elvish_Hunter! I will soon experiment with it a bit.. will write here if anything interesting
Re: enclave's Lua thread
Sorry for being a bit off topic, but...
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".Elvish_Hunter wrote:Protip: if you put a definition = "radio" inside a T.toggle_button, it'll look like a proper radiobutton.
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
| Abandoned: Tales of the Setting Sun
GitHub link for these projects
Re: enclave's Lua thread
Thats becasue those definitions are defined in wml.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".
You can check wesnoths core dialogs, most of them also use gui2.doofus-01 wrote: Where do you find such things?
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.
- Elvish_Hunter
- Posts: 1575
- Joined: September 4th, 2009, 2:39 pm
- Location: Lintanir Forest...
Re: enclave's Lua thread
Exactly as gfgtdf said, I found it by reading the content of every cfg file indoofus-01 wrote:Where do you find such things?
data/gui/default/window/
. I ended up making a small text file (very confused, but nobody else will read it anyway ) 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)
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)