Dugi's lua questions

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

Moderator: Forum Moderators

User avatar
Xudo
Posts: 563
Joined: April 3rd, 2009, 5:26 pm

Re: Question: wml versus lua in matters of variable operatio

Post by Xudo »

@Dugi
I think that your reasons "to keep scenarios, item lists, skills' properties and so on in WML" comes from misunderstanding of "lua storage" concept.

EDIT: Though I agree with storing scenarios in WML. But everything else CABD faster in lua.
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Question: wml versus lua in matters of variable operatio

Post by Anonymissimus »

Xudo wrote:Is it possible to create variables in special namespace instead of $unit ?.
Not sure what you are asking, but it's possible to have sort-of arbitrary "automatically scoped" wml variables within custom events, namely by misusing the automatic $weapon and $second_weapon variables, which allow you to pass arbitrary wml tables/config objects/a block of wml. See the second part in this post http://forums.wesnoth.org/viewtopic.php ... 66#p510066 for an example.
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
Dugi
Posts: 4961
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Question: wml versus lua in matters of variable operatio

Post by Dugi »

Thanks for your replies, that's all I needed to know, but await a lot of replies soon because I will start working on it within a few days.
User avatar
Dugi
Posts: 4961
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Question: wml versus lua in matters of variable operatio

Post by Dugi »

Question. I am trying to make a GUI, and I need a text (in label) to be separated into multiple lines. I have set characters_per_line to 30, but it is not getting separated. I have tried to limit the resolution of the window, but it just added a scrollbar. Any ideas?

The object whose text I want to separate into lines is named gui_class_description .

Code: Select all

local dialog = { maximum_width=800, maximum_height=600 ,
	  T.tooltip { id = "tooltip_large" },
	  T.helptip { id = "tooltip_large" },
	  T.grid { T.row {
	    T.column { T.grid { T.row { T.column { T.label { id="title" , definition="title" , label="Select hero class"} } }, 
		T.row{ T.column { horizontal_grow = "false" , grow_factor=1 , horizontal_alignment = "left" , vertical_alignment = "top" , border= "All" , border_size = 5, T.label { id = "gui_class_description", label="Necromancer is a specialist on raising undead minions and crippling enemies with various curses and poisons, with a skeletal figure affecting reistances. Necromancers are specialist spellcasters, and generally weak in melee combat." , characters_per_line = 30 } }} ,
	      T.row { T.column { horizontal_grow = true, T.listbox { id = "gui_character_chosen", horizontal_scrollbar_mode = "never" ,
		T.list_definition { T.row { T.column { horizontal_grow = true,
		  T.toggle_panel { T.grid { T.row {
		    T.column { horizontal_alignment = "left", T.label { id = "the_label" } },
		    T.column { T.image { id = "the_icon" } }
		  } } }
		} } }
	      } } },
	      T.row { T.column { T.grid { T.row {
		T.column { T.button { id = "ok", label = "Choose" } },
		T.column { T.button { id = "cancel", label = "Back" } }
	      } } } }
	    } }
	  } }
	}
Another question: are variable indexes working in the same way as in WML? Because I have seen something like this:

Code: Select all

local foo = { "v1" , "v2" , "v3" }
And I have no idea how could it look in WML.
Luther
Posts: 128
Joined: July 28th, 2007, 5:19 pm
Location: USA

Re: Question: wml versus lua in matters of variable operatio

Post by Luther »

Dugi wrote:Question. I am trying to make a GUI, and I need a text (in label) to be separated into multiple lines. I have set characters_per_line to 30, but it is not getting separated. I have tried to limit the resolution of the window, but it just added a scrollbar. Any ideas?
I haven't tried doing any GUI stuff. What Wesnoth version are you using? I was under the impression that 1.10 can't do this kind of GUI-related stuff.
Dugi wrote: Another question: are variable indexes working in the same way as in WML? Because I have seen something like this:

Code: Select all

local foo = { "v1" , "v2" , "v3" }
And I have no idea how could it look in WML.
That's not a valid WML table. It's equivalent to '{ [1]="v1" , [2]="v2" , [3]="v3" }'. WML can do an array of records, but not of simple strings. Assuming you want an array of strings, here are some ways to do it:

Code: Select all

-- More traditional, but very verbose. This method is necessary if you need
-- multiple fields per item.
{
  T.item{
    value = 'v1'
  },
  T.item{
    value = 'v2'
  },
  T.item{
    value = 'v3'
  }
}


-- I prefer this:
{
  _1 = 'v1',
  _2 = 'v2',
  _3 = 'v3'
}


-- Some people use constructed variable names.
function getScalarFromList(listName, index)
  return V[listName .. index]
end

function setScalarInList(listName, index, value)
  V[listName .. index] = value
end

setScalarInList('myArray', 1, 'v1') -- Equivalent to V.myArray1 = 'v1'
setScalarInList('myArray', 2, 'v2')
setScalarInList('myArray', 3, 'v3')
User avatar
Dugi
Posts: 4961
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

Thanks, Luther.

Quick question (for somebody who knows to do stuff with GUI):
Why this does tell me that a grid cell does not exactly have 1 child?

Code: Select all

	local dialog = { maximum_width=800, maximum_height=600 , T.tooltip { id = "tooltip_large" }, T.helptip { id = "tooltip_large" },
		T.grid {
			T.row { T.column { T.label { id="title" , definition="title" , label="Create your hero"} }} ,
			T.row { T.column { T.grid {
				T.row { T.column { horizontal_grow = "false" , grow_factor=1 , horizontal_alignment = "left" , vertical_alignment = "top" , border= "All" , border_size = 5, T.label { id = "gui_class_description", label="Necromancer is a specialist on raising undead minions and crippling enemies with various curses and poisons, with a skeletal figure affecting reistances. Necromancers are specialist spellcasters, and generally weak in melee combat." , characters_per_line = 30 } } }},
					T.row { T.column { horizontal_grow = true, T.listbox { id = "gui_character_chosen", horizontal_scrollbar_mode = "never" ,
						T.list_definition { T.row { T.column { horizontal_grow = true,
							T.toggle_panel { T.grid { T.row {
								T.column { horizontal_alignment = "left", T.label { id = "gui_class_name" } },
								T.column { T.image { id = "gui_class_icon" } }
							 } } }
						} } }
					} } }
				}}}
			},
			T.row { T.column { T.grid { T.row {
				T.column { T.button { id = "ok", label = "Choose" } },
				T.column { T.button { id = "cancel", label = "Back" } }
			}}}}
And I am on wesnoth 1.10, it seems to support GUI well.

This GUI was supposed to write several lists in two columns, with a title and two buttons bellow. The second column is not written yet because it didn't work without it.

And sorry for asking a lot, I feel like a total noob now.
User avatar
Xudo
Posts: 563
Joined: April 3rd, 2009, 5:26 pm

Re: Dugi's lua questions

Post by Xudo »

If you show example of interface drawn in any graphical editor, then it will be easier to figure out what do you need.
User avatar
Dugi
Posts: 4961
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

It was reporting an error, and I have verified that there is no ambiguity in that, so screenshots aren't really needed. The line breaking problem should be also explanatory enough, I would have posted screenshots, but I cannot do it right now (I'm not on Linux). I will post them asap.
User avatar
Elvish_Hunter
Posts: 1576
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: Dugi's lua questions

Post by Elvish_Hunter »

Right now, I can't test your window (I'll do it ASAP), but I can give you a suggestion: create macrowidgets. For example, the last T.grid with the two buttons becomes a variable called buttonbox:

Code: Select all

local buttonbox = T.grid { T.row {
            T.column { T.button { id = "ok", label = "Choose" } },
            T.column { T.button { id = "cancel", label = "Back" } }
         }}
so you can call it:

Code: Select all

T.row { T.column { buttonbox } }
This will make your code easier to read, and also easier to isolate the offending chunks.
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
Dugi
Posts: 4961
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

I tried it with a high organisation of spaces and brackets, and managed to make something that works to a certain extent.

The problem with 'a grid cell does not exactly have 1 child' appeared when I left an empty row or column, planning to add something after trying out the part I had already written.

I have made this window as I wanted it to be, but I still have the problem with line-breaking. I have no idea how to force the labels to break into multiple lines.

So, this is if I try to write all values to fit the sizes:
07.png
This is how it looks if I write the class description properly (causing it to be way too long if there is no line breaking):
39.png
This is the code of the whole interactive window:
Spoiler:
User avatar
Elvish_Hunter
Posts: 1576
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: Dugi's lua questions

Post by Elvish_Hunter »

Dugi wrote:I tried it with a high organisation of spaces and brackets, and managed to make something that works to a certain extent.
I too did something similar. What I did was splitting it in some macrowidgets, rework the indentation in a sort-of C++ style and fixing some brackets when an error was raised. Now it works.
Window code

Code: Select all

function wml_actions.dugi_dialog( cfg )
	local buttonbox = T.grid {
						T.row {
							T.column {
								T.button {
									id = "ok",
									label = "Choose"
								} 
							},
							T.column {
								T.button {
									id = "cancel",
									label = "Back"
								}
							}
						}
					}

	local hero_label =  T.row {
							T.column {
								horizontal_grow = "false",
								grow_factor=1,
								horizontal_alignment = "left",
								vertical_alignment = "top",
								border= "All",
								border_size = 5,
								T.scroll_label {
									id = "gui_class_description",
									label="Necromancer is a specialist on raising undead minions and crippling enemies with various curses and poisons, with a skeletal figure affecting reistances. Necromancers are specialist spellcasters, and generally weak in melee combat."
								} 
							}
						}

	local hero_listbox = T.row {
							T.column { 
								horizontal_grow = true,
								T.listbox { 
									id = "gui_character_chosen",
									horizontal_scrollbar_mode = "never" ,
									T.list_definition { 
										T.row {
											T.column {
												horizontal_grow = true,
												T.toggle_panel { 
													T.grid {
														T.row {
															T.column { 
																horizontal_alignment = "left",
																	T.label { 
																		id = "gui_class_name"
																	}
															},
															T.column {
																T.image {
																	id = "gui_class_icon"
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}

   local dialog = {
					maximum_width=800,
					maximum_height=600,
					T.tooltip { id = "tooltip_large" },
					T.helptip { id = "tooltip_large" },
					T.grid {
						T.row {
							T.column {
								T.label {
									id="title",
									definition="title",
									label="Create your hero"
								}
							}
						},
						T.row {
							T.column {
								T.grid {
									hero_label,
									hero_listbox
								}
							}
						},
						T.row {
							T.column { buttonbox }
						}
					}
				}
	
	wesnoth.show_dialog( dialog )
end
Dugi wrote:I have no idea how to force the labels to break into multiple lines.
Well, you can look to the scroll_label widget. Don't worry about scrollbars, because they won't show up unless they're necessary. In the above code I replaced the label with a scroll_label, for example.
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
Dugi
Posts: 4961
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

Well, you can look to the scroll_label widget. Don't worry about scrollbars, because they won't show up unless they're necessary. In the above code I replaced the label with a scroll_label, for example.
Thanks a lot!
I really wonder why I didn't find it earlier, I have skimmed through that page at least 10 times.

EDIT: Bummer. Two scroll_label-s cannot be used in a single window if their text is too long. This is how it looks when if only one text is longer than the space it is given:
47.png
This is how it will look when two of them require the text compressing feature of scroll_label :
Both scroll_label-s uncompressed.
Both scroll_label-s uncompressed.
EDIT #2:How do I change the tooltip property in GUI by callback events (workaround attempt)?
I tried (entering dialog, finding grid, finding the second row in it, the second column in it, then the grid inside it,...):

Code: Select all

dialog.grid.row[2].column[2].grid.row[3].column.listbox.tooltip = "blablabla"
or this (just hoping it might work):

Code: Select all

dialog.grid[2].row[2].column.grid[3].row.column.listbox.tooltip = "blablabla"
But it does not seem to work, telling me about some nil values and bad indexes (related to the code I posted above). I have a feeling I am trying to set variables deep within arrays in a completely wrong way.
User avatar
Dugi
Posts: 4961
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

Bump.

Sorry for bumping, but I need my questions, at least the one from EDIT#2 answered (as it should not be hard to answer), and probably nobody noticed I edited my last post.

I also need to ask one more question, would it be feasible if I wrote many essential data-holding variable arrays in WML as the contents of an otherwise unused dummy unit type, that would be stored by lua code, turned into lua variables and then used? It would be the contents of [abilities] or [event] tags in the unit's file.
This way I would avoid storing these variables inside save files, and allow other people to easily edit it.
User avatar
Crendgrim
Moderator Emeritus
Posts: 1328
Joined: October 15th, 2010, 10:39 am
Location: Germany

Re: Dugi's lua questions

Post by Crendgrim »

It should be possible, yes.
Just to name a different approach: In my campaign, I have created data files for easier editing and maintaining of areas as well. I then load the file via a WML preprocessor include and handle its contents to a Lua function, which then parses the file. Depending on how your variables are going to look, this might be an even easier way.
In case you're interested, here's the important files
data/area/C1_Village.qow — an example data file
lua/map-utils.lua — with the first two functions for parsing the data – sure, it's a bit overcomplicated, but still
The actual inclusion is scattered around the utils/ directory.
UMC Story Images — Story images for your campaign!
User avatar
Dugi
Posts: 4961
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

Hm, I understand the way it reads the data, but I need to make it support WML stuff, like subtags and macro substitutions. But this would be also quite useful.

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 and I am failing to google it. Workaround with wesnoth.set_variable kinda kills the purpose.
Post Reply