vultraz's lua questions

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

Moderator: Forum Moderators

Post Reply
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: vultraz's lua questions

Post by 8680 »

A few more comments.

This:

Code: Select all

if result == true then
Can be simplified to this:

Code: Select all

if result then
And since you don't use result anywhere else, this:

Code: Select all

local result = wesnoth.eval_conditional { { "have_unit", { canrecruit = "true", side = boss_side } } }
if result then
Can be simplified to this:

Code: Select all

if wesnoth.eval_conditional { { "have_unit", { canrecruit = "true", side = boss_side } } } then
Finally, while you can use string.format(), you don't have to.
User avatar
vultraz
Developer
Posts: 960
Joined: February 7th, 2011, 12:51 pm
Location: Dodging Daleks

Re: vultraz's lua questions

Post by vultraz »

ok... umm... well because of complications, I was forced to move to the addon to SP ans scrap the plans I had for this nice unit death system. So the lua I was written isn't needed now :( (I saved it though)
Right now I'm working on my inventory, yet another thing I realized I have to do before I tackle the shop, and:

Code: Select all

function wesnoth.wlm_actions.inventory_item(cfg)
	local u_id = wesnoth.get_variable("unit.id")
	local u_index = wesnoth.get_variable("inventory." .. unit_id .. ".length") + 1
	local var_path = "inventory." .. u_id .. u_index .. 

	wesnoth.set_variable(var_path .. ".name", cfg.name)
	wesnoth.set_variable(var_path .. ".id", cfg.id)
	wesnoth.set_variable(var_path .. ".image", cfg.image)
	wesnoth.set_variable(var_path .. ".status", cfg.status)
	wesnoth.set_variable(var_path .. ".command", helper.get_child(cfg, "command"))
end
gives:
Picture 26.png
Help please.
Creator of Shadows of Deception (for 1.12) and co-creator of the Era of Chaos (for 1.12/1.13).
SurvivalXtreme rocks!!!
What happens when you get scared half to death...twice?
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: vultraz's lua questions

Post by mattsc »

Read your very first line carefully, especially the order of letters like 'l' and 'm'. :)
User avatar
vultraz
Developer
Posts: 960
Joined: February 7th, 2011, 12:51 pm
Location: Dodging Daleks

Re: vultraz's lua questions

Post by vultraz »

:doh: :doh:
OH GAWD...How did I not see that?!
Creator of Shadows of Deception (for 1.12) and co-creator of the Era of Chaos (for 1.12/1.13).
SurvivalXtreme rocks!!!
What happens when you get scared half to death...twice?
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: vultraz's lua questions

Post by 8680 »

Besides the spelling error, the fourth line should probably be:

Code: Select all

-- note brackets around index and lack of stray concatenation operator
local var_path = "inventory." .. u_id .. "[" .. u_index .. "]"
Or:

Code: Select all

 -- this might be more easily readable
local var_path = ("inventory.%s[%i]"):format(u_id, u_index)
User avatar
vultraz
Developer
Posts: 960
Joined: February 7th, 2011, 12:51 pm
Location: Dodging Daleks

Re: vultraz's lua questions

Post by vultraz »

Code: Select all

local item_action = helper.get_child(cfg, "command")
local remove_action = helper.get_child(cfg, "remove_command")

wesnoth.set_variable(var_path .. ".command", item_action.__cfg)
wesnoth.set_variable(var_path .. ".remcommand", remove_action.__cfg)
Am I doing that right? Because those vars don't gets set.

An example of usage:

Code: Select all

[inventory_item]
    id=scen1_key
    name= _ "Key"
    image=misc/x.png
    description= _ "An old, but well taken care of, key. It appears to be down here for safekeeping."
    action= _ "This key can be used to open a locked gate."
    section=items
    status=shared
    [command]
        {VARIABLE special_key.present no}
    [/command]
    [remove_command]
        {VARIABLE special_key.present yes}
    [/remove_command]
[/inventory_item]
What I'm trying to do is have two vars, one with code that is applied when you pick up the item or get it from the shared section of the inventory, and the other with the code that's the opposite-get's applied when you drop an item on the map or moved from the personal section to the shared section of the inventory. I know it's sorta clumsy, but if anyone has any suggestions on how I could have item's effects removed and applied at will, please tell me. This is the best I could come up with. :hmm:
Creator of Shadows of Deception (for 1.12) and co-creator of the Era of Chaos (for 1.12/1.13).
SurvivalXtreme rocks!!!
What happens when you get scared half to death...twice?
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: vultraz's lua questions

Post by Anonymissimus »

Where does it say that the parameter passed to a wml tag implementation has a __cfg field ? What you probably want is item_action.__literal etc.
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
vultraz
Developer
Posts: 960
Joined: February 7th, 2011, 12:51 pm
Location: Dodging Daleks

Re: vultraz's lua questions

Post by vultraz »

Ok working on a custom window for the inventory now.

Code: Select all

	local item_list = T.listbox { id = "inventory_list",
				      definition = "default",
				      T.list_definition { T.row { T.column { vertical_grow = "true",
								 	     horizontal_grow = "true",
									     T.toggle_panel { T.grid { T.row { T.column { grow_factor = 0,
															  horizontal_alignment = "left",
															  border = "all",
															  border_size = 5,
														          T.toggle_button { id = "checkbox",
																	    definition = "default",
																	    linked_group = "checkbox" } },
													       T.column { grow_factor = 1,
															  horizontal_grow = "true",
															  border = "all",
															  border_size = 5,
														          T.image { id = "image",
																    definition = "default" } },
													       T.column { grow_factor = 1,
															  horizontal_grow = "true",
															  border = "all",
															  border_size = 5,
															  T.label { id = "name",
																    definition = "default",
																    linked_group = "name" } } } } } } } } }
I'd like to have 1 option (contents of T.toggle_panel I guess) for each index (each of which contains 1 item's stat vars) in an array. Shadowmaster said to:
<vultraz> shadowmaster: Ok got it. Just one more thing, I'd like to have the options print out from an array, where each index is a separate item. how can i do that?
<shadowmaster> iterate over the array and push items into the listbox in the loop block?
<vultraz> shadowmaster: ????? I get the first part (FOREACH?). but the second..
<shadowmaster> vultraz: push items into the listbox in the loop block?
<vultraz> yeah
<shadowmaster> that means that in the loop body you'll issue a call to whatever function is used to insert rows at the end of the listbox
<shadowmaster> don't ask me about how to do that in Lua for I have only programmed with GUI2 in C++
But I have no idea how. Can someone give me some pointers? Thanks :D
Creator of Shadows of Deception (for 1.12) and co-creator of the Era of Chaos (for 1.12/1.13).
SurvivalXtreme rocks!!!
What happens when you get scared half to death...twice?
User avatar
Elvish_Hunter
Posts: 1576
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: vultraz's lua questions

Post by Elvish_Hunter »

First of all, I never experimented with toggle_panel so far, hence I cannot provide you a chunk of code right now :(
That said, a listbox requires both a list_definition and a list_data, which is missing in your code.
vultraz wrote:I'd like to have 1 option (contents of T.toggle_panel I guess) for each index (each of which contains 1 item's stat vars) in an array. Shadowmaster said to:
My guess is to use helper.get_variable_array to read the array's content. Then the problem will be to set as many lines in the listbox as the array's indexes. This one may be made by making a temp_list_data = {} table, and using table.insert() for each array index. Then, in the dialog definition, you'll use T.list_data temp_list_data.
EDIT: I'm using a listbox in the Wesnoth Lua Pack, you may want to take this as example:

Code: Select all

		local facing_radiobutton = T.horizontal_listbox { id = "facing_listbox",
								  T.list_definition { T.row { T.column { T.toggle_button { id = "facing_radiobutton" } } } },
													 T.list_data {
															T.row { horizontal_alignment = "left",
															       border = "all",
															       border_size = 5,
															       T.column { label = _"nw" } },
															T.row { horizontal_alignment = "left",
															       border = "all",
															       border_size = 5,
															       T.column { label = _"ne" } },
															T.row { horizontal_alignment = "left",
															       border = "all",
															       border_size = 5,
															       T.column { label = _"n" } },
															T.row { horizontal_alignment = "left",
															       border = "all",
															       border_size = 5,
															       T.column { label = _"sw" } },
															T.row { horizontal_alignment = "left",
															       border = "all",
															       border_size = 5,
															       T.column { label = _"se" } },
															T.row { horizontal_alignment = "left",
															       border = "all",
															       border_size = 5,
															       T.column { label = _"s" } }
								} }
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
vultraz
Developer
Posts: 960
Joined: February 7th, 2011, 12:51 pm
Location: Dodging Daleks

Re: vultraz's lua questions

Post by vultraz »

Ok, so should I put the array stuff in the preshow function?

One other thing, the wiki says the helper.get_variable_array Fetches all the WML container variables with given name and returns a table containing them (starting at index 1). Doesn't that mean it won't get any items stored in the 0 index?
Creator of Shadows of Deception (for 1.12) and co-creator of the Era of Chaos (for 1.12/1.13).
SurvivalXtreme rocks!!!
What happens when you get scared half to death...twice?
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: vultraz's lua questions

Post by Anonymissimus »

vultraz wrote:One other thing, the wiki says the helper.get_variable_array Fetches all the WML container variables with given name and returns a table containing them (starting at index 1). Doesn't that mean it won't get any items stored in the 0 index?
get/set_variable_array perform the index shift for you, no worry. :)
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
vultraz
Developer
Posts: 960
Joined: February 7th, 2011, 12:51 pm
Location: Dodging Daleks

Re: vultraz's lua questions

Post by vultraz »

Something like this?

Code: Select all

local function item_preshow()		 
		temp_list_data = { name = { }, image = { } }
		function inventory_category_scan(section)
			u_id = wesnoth.get_variable("unit.id")
			i = 0 

			repeat 
				table.insert(temp_list_data.name, i , wesnoth.get_variable( u_id .. section  .. "[" .. i .. "].name"))
				table.insert(temp_list_data.image, i , wesnoth.get_variable( u_id .. section .. "[" .. i .. "].image"))

				i = i + 1
			until i == wesnoth.get_variable( u_id .. section .. ".length")
		end

		inventory_category_scan(".items")
		inventory_category_scan(".weapons")
		inventory_category_scan(".potions")

		wesnoth.set_dialog_value ( "DESCRIPTION PLACEHOLDER", "description" )
		wesnoth.set_dialog_value ( "Switch items", "switch_button" )
		wesnoth.set_dialog_value ( "Leave in chest", "leave_chest_button" )
		wesnoth.set_dialog_value ( "Exit", "ok_button" )
	end
Also, what should I set the ids of the T.image and T.label to so that they will actually appear? Currently they're set to 'image' and 'name', respectively. As there are variables in that in temp_list_data (see above), should they automatically fill in? And what do the linked_group= keys do?
Creator of Shadows of Deception (for 1.12) and co-creator of the Era of Chaos (for 1.12/1.13).
SurvivalXtreme rocks!!!
What happens when you get scared half to death...twice?
Exasperation
Posts: 462
Joined: June 8th, 2006, 3:25 am

Re: vultraz's lua questions

Post by Exasperation »

You need to either use wesnoth.set_dialog_value to insert the values into the widgets like so:

Code: Select all

local function item_preshow()
	local u_id = wesnoth.get_variable("unit.id") -- just to check, is unit a WML variable which contains the name of another WML variable?
	local function inventory_category_scan(section) -- changed function name to match usage
		for i = 1, wesnoth.get_variable(string.format("%s.%s.length", u_id, section)) do -- using .. works too, but I personally prefer string.format for all but the simplest uses
			-- additionally, should this be "%s.variables.%s.length"? (assuming u_id holds the name of a WML variable that contains a stored unit here)
			-- also, a for loop seems simpler here, since it only needs to perform the get_variable once
			wesnoth.set_dialog_value(wesnoth.get_variable(string.format("%s.%s[%d].name", u_id, section, i - 1)), "inventory_list", i, "name")
			wesnoth.set_dialog_value(wesnoth.get_variable(string.format("%s.%s[%d].image", u_id, section, i - 1)), "inventory_list", i, "image")
		end
	end
	inventory_category_scan("items")
	inventory_category_scan("weapons")
	inventory_category_scan("potions")
	wesnoth.set_dialog_value("DESCRIPTION PLACEHOLDER", "description")
	wesnoth.set_dialog_value("Switch items", "switch_button")
	wesnoth.set_dialog_value("Leave in chest", "leave_chest_button")
	wesnoth.set_dialog_value("Exit", "ok_button")
end
or set up the dialog definition dynamically by building a list of list_data entries based on the inventory like so:

Code: Select all

local list_data = {}
local u_id = wesnoth.get_variable("unit.id")
local function inventory_category_scan(section)
	for i = 0, wesnoth.get_variable(string.format("%s.%s.length", u_id, section)) - 1 do
		table.insert(list_data, T.row { T.column {<build appropriate toggle panel data here based on i>} })
	end
end
inventory_category_scan("items")
inventory_category_scan("weapons")
inventory_category_scan("potions")

local item_list = T.listbox {
	id = "inventory_list",
	definition = "default",
	T.list_definition { T.row { T.column {
		vertical_grow = "true",
		horizontal_grow = "true",
		T.toggle_panel { T.grid { T.row {
			T.column {
				grow_factor = 0,
				horizontal_alignment = "left",
				border = "all",
				border_size = 5,
				T.toggle_button {
					id = "checkbox",
					definition = "default",
					linked_group = "checkbox" } },
			T.column {
				grow_factor = 1,
				horizontal_grow = "true",
				border = "all",
				border_size = 5,
				T.image {
					id = "image",
					definition = "default" } },
			T.column {
				grow_factor = 1,
				horizontal_grow = "true",
				border = "all",
				border_size = 5,
				T.label {
					id = "name",
					definition = "default",
					linked_group = "name" } } } } } } } },
	T.list_data list_data }
The list_data section is not mandatory (it defaults to empty), but if you don't use a list_data section you will need to use set_dialog_value to insert your data from the preshow function (or potentially from a callback that's set in the preshow function, but that doesn't seem to be necessary in this case).

Just a further note on toggle panels; I haven't experimented with them either, they may be one of the widgets that is not yet fully supported by the Lua dialog interface (the only one I am currently sure isn't fully supported is the multipage widget). If they are, full support for them will probably have to wait for 1.11 because of the 1.9 feature freeze.
User avatar
vultraz
Developer
Posts: 960
Joined: February 7th, 2011, 12:51 pm
Location: Dodging Daleks

Re: vultraz's lua questions

Post by vultraz »

Ok I'm trying out the first one as the second confuses me, and I get this after clicking the menu after picking up an item:
Error:
As for the array structure, each main character has an array in his id that contains variables used by things like the inventory and shop. So an item here might be in, for example:

Code: Select all

Rhyan.items[0].<stat code, eg name, section, description, image, etc>
That's why I used unit.id to fill in the first part of the array based on who was using it at the time. There's also an separate array called shared_items that follows the same structure. I'm going to have to expand the function to include it, as the scared stuff is supposed to print in the same manner as the personal stuff, but in a different T.column.
Creator of Shadows of Deception (for 1.12) and co-creator of the Era of Chaos (for 1.12/1.13).
SurvivalXtreme rocks!!!
What happens when you get scared half to death...twice?
Exasperation
Posts: 462
Joined: June 8th, 2006, 3:25 am

Re: vultraz's lua questions

Post by Exasperation »

Ok, so unit.id does give you the name of a WML variable, and it's not storing the actual unit, so no .variable needed. Just wanted to make sure.

The error you're getting says that I got the identifying path to the widget wrong, which isn't too surprising since I'm going by the table you posted a few posts earlier, which doesn't seem to be the entire table you're using (your preshow references widgets with ids "description", "switch_button", "ok_button", and "chest_button", none of which exists in the table that you posted; also that table doesn't include the outer grid, tooltip, and helptip parts). That just means you have to correct the path I gave to match the full WML description of the dialog that you're actually using. At a guess, is "inventory_list" not a unique widget identifier?

Looking in the wiki, the big example from http://wiki.wesnoth.org/LuaWML:Display# ... alog_value actually uses this same technique for populating a listbox, I could have just pointed there. :doh:
Post Reply