vultraz's lua questions

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

Moderator: Forum Moderators

Post Reply
User avatar
vultraz
Developer
Posts: 960
Joined: February 7th, 2011, 12:51 pm
Location: Dodging Daleks

Re: vultraz's lua questions

Post by vultraz »

Humm...but...wouldn't wesnoth.get_dialog_value something to do with the image or name, as those are the values that are put into the T.image and T.label widgets? Those are just two of the values stored in each item's array; should I be abled to access them all of I use this?

Also, wesnoth.get_dialog_value would need to work based on which listbox option was selected at the time. Maybe a wesnoth.set_dialog_callback in the for loop (the one that prints the listbox options)? But then again, would that work when selecting a listbox option, if it's used in a loop which inputs image and label widgets?

I'm not sure if I'm making much sense, but I'm quite confused as to how all this works.
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 can set a callback function for a listbox, which will then be called whenever any listbox option is selected. Also, if you call wesnoth.get_dialog_value with the path to a listbox, it will return the index of the currently selected option. So you can do something like this (as always, check to see if the widget paths need correcting):

Code: Select all

local function select_from_inventory()
	local i = wesnoth.get_dialog_value("inventory_list") -- get the index of the currently selected list member
	local image = wesnoth.get_dialog_value("inventory_list", i, "list_image") -- get the image used by the currently selected list member
	wesnoth.set_dialog_value(image, "image") -- and set the image at the bottom to be the same
	local label = wesnoth.get_dialog_value("inventory_list", i, "list_name") -- now do the same for the label
	wesnoth.set_dialog_value(label, "description")
	-- insert any other things that need to be updated here
end
wesnoth.set_dialog_callback(select_from_inventory, "inventory_list") -- set the function to be called whenever a member of the list is clicked on
If you have some way of keeping track of which WML array element corresponds to which list element, you can also get access to the extra data in the WML array (for example, you can keep a lua table where the table's element i contains the path to the item information in WML corresponding to list element i, then you can do things like wesnoth.get_variable(path_table .. ".in_use") to get the extra info, regardless of whether the items are in the same array, different arrays, separate non-array containers, or whatever - just populate path_table at the same time as you populate the listbox). Just keep in mind that if you set a WML variable inside of your dialog, it will not be synchronized, and you will have to synchronize it with the other clients separately (it should be possible to synchronize a WML variable using global variables).
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: vultraz's lua questions

Post by Anonymissimus »

Exasperation wrote:Just keep in mind that if you set a WML variable inside of your dialog, it will not be synchronized, and you will have to synchronize it with the other clients separately (it should be possible to synchronize a WML variable using global variables).
Err, the way one should do things like that is wesnoth.synchronize_choice.

Code: Select all

	local sync_helper = function()
		--This sync_helper function is executed only on the client with side $side_number and the wml table it returns is transferred to the other clients.
		--The return value *has* to be a wml table (for synchronize_choice to return non-nil).
		return { value = some_function_doing_what_you_want_and_returning_the_result() }
	end
	local value = wesnoth.synchronize_choice(sync_helper).value
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: vultraz's lua questions

Post by Exasperation »

E_H was recommending using wesnoth.set_variable or helper.set_variable_array within the dialog (that is, inside the synchronize_choice function). Trying to synchronize that with the synchronize_choice return value is somewhat more complicated than using global variables to do so afterwards (although either can work). Personally, I would try to keep set_variable outside of synchronize_choice altogether (by relying on local variables), since using it inside synchronize_choice can lead to OOS.
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, well, I've applied those suggestions and get a weird error when I click the right-click menu option. It does't say that the widget isn't found, it says it's unsupported.
Picture 26.png

Code: Select all

-- This brings up the custom inventory control window
function wml_actions.inventory_controller(cfg)
	local item_list_personal = T.listbox { id = "inventory_list_personal",
				               definition = "default",
				               T.scrollbar { T.grid { T.row { T.column { T.vertical_scrollbar { } } } } },
				               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 = "list_checkbox",
																	             definition = "default",
																	             linked_group = "checkbox" } },
													                T.column { grow_factor = 1,
															           horizontal_grow = "true",
															           border = "all",
															           border_size = 5,
														                   T.image { id = "list_image",
																             definition = "default",
																             linked_group = "name" } },
													                T.column { grow_factor = 1,
															           horizontal_grow = "true",
															           border = "all",
															           border_size = 5,
															           T.label { id = "list_name",
																             definition = "default",
																             linked_group = "name" } } } } } } } } }

	local item_list_shared = T.listbox { id = "inventory_list_shared",
				             definition = "default",
				             T.scrollbar { T.grid { T.row { T.column { T.vertical_scrollbar { } } } } },
				             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 = "list_checkbox",
							    											   definition = "default",
							    											   linked_group = "checkbox" } },
			       											      T.column { grow_factor = 1,
				         										         horizontal_grow = "true",
					  											 border = "all",
					  											 border_size = 5,
					 											 T.image { id = "list_image",
					            										 	   definition = "default",
						    											   linked_group = "name" } },
			       											      T.column { grow_factor = 1,
					 											 horizontal_grow = "true",
					  											 border = "all",
					 											 border_size = 5,
					  											 T.label { id = "list_name",
						    											   definition = "default",
						    											   linked_group = "name" } } } } } } } } }
	
	local main_window = { T.helptip { id="tooltip_large" }, -- mandatory field												
			      T.tooltip { id="tooltip_large" }, -- mandatory field
			      T.linked_group { id = "image",
					       fixed_height = "true",
					       fixed_width = "true" },
			      T.linked_group { id = "checkbox",
				    	       fixed_height = "true",
					       fixed_width = "true" },
			      T.linked_group { id = "name",
					       fixed_height = "true",
					       fixed_width = "false" }, 
			      height = 600,
			      width = 700,	
			      T.grid { T.row { growth_factor = 1, 
					       T.column { grow_factor = 1,
						          horizontal_alignment = "left",
							  border = "all",
							  border_size = 5,
							  T.label { definition = "title", 
								    label = _"Inventory" } } },
				       T.row { T.column { T.grid { T.row { T.column { item_list_personal },
									   T.column { T.spacer { definition = default,
												 width=10 } },
									   T.column { item_list_shared } } } } },
				       T.row { T.column { T.grid { T.row { T.column { vertical_alignment = "center",
							  			      horizontal_alignment = "center",
							  			      border = "all",
							  			      border_size = 5, 
							  			      T.image { id = "details_image" } },
					       				   T.column { vertical_alignment = "top",
							  			      horizontal_alignment = "center",
							  			      border = "all",
							  			      border_size = 5,
										      T.grid { T.row { T.column { T.label { definition = "title", 
															    id = "details_name" } } },
											       T.row { T.column { T.label { id = "details_description" } } } } },
					       				   T.column { T.grid { T.row { T.column { T.button { id = "switch_button", return_value = 3 } } }, 
								   		               T.row { T.column { T.button { id = "leave_chest_button", return_value = 2 } } },
								   		               T.row { T.column { T.button { id = "ok_button", return_value = 1 } } } } } } } } }		  
				      } }       
	
	local function item_preshow()
		local u_id = wesnoth.get_variable("unit.id")
		local function inventory_category_scan(section, list)
			for i = 1, wesnoth.get_variable(string.format("%s.%s.length", u_id, section)) do

			wesnoth.set_dialog_value(wesnoth.get_variable(string.format("%s.%s[%d].image", u_id, section, i - 1)), list, i, "list_image")
			wesnoth.set_dialog_value(wesnoth.get_variable(string.format("%s.%s[%d].name", u_id, section, i - 1)), list, i, "list_name")

			end
		end
		inventory_category_scan("items", "inventory_list_personal")
		inventory_category_scan("weapons", "inventory_list_personal")
		inventory_category_scan("potions", "inventory_list_personal")

		local u_id = "shared"

		inventory_category_scan("items", "inventory_list_shared")
		inventory_category_scan("weapons", "inventory_list_shared")
		inventory_category_scan("potions", "inventory_list_shared")

		local function select_from_inventory(list)
			local i = wesnoth.get_dialog_value(list)
			local d_image = wesnoth.get_dialog_value(list, i, "list_image")
			wesnoth.set_dialog_value(d_image, "details_image")

			local d_name = wesnoth.get_dialog_value(list, i, "list_name")
			wesnoth.set_dialog_value(d_name, "details_name")
			-- insert any other things that need to be updated here
		end

		wesnoth.set_dialog_callback(select_from_inventory("inventory_list_personal"), "inventory_list_personal")
		wesnoth.set_dialog_callback(select_from_inventory("inventory_list_shared"), "inventory_list_shared")

		wesnoth.set_dialog_value("DESCRIPTION PLACEHOLDER", "details_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

	local function sync()
		local function item_postshow()
		end

		local return_value = wesnoth.show_dialog( main_window, item_preshow, item_postshow )

		return { return_value = return_value }
	end

	local return_table = wesnoth.synchronize_choice(sync)
end
Thanks for your continued help. :)
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 »

Argh, there's currently no support for getting the value of a label (or by extension an image) widget through Lua. :doh: Unfortunately, support for this can't be added during the feature freeze, so it looks like you're going to have to store that information in an additional place where you can access it easily, something like so:

Code: Select all

   local function item_preshow()
      local u_id = wesnoth.get_variable("unit.id")
	  local personal_inv_list_data = {}
      local function inventory_category_scan(section, list)
         local var = u_id .. section
         for i = 1, wesnoth.get_variable(var .. "length") do
		    table.insert(list, { image = wesnoth.get_variable(string.format("%s[%d].image", var, i - 1)), name = wesnoth.get_variable(string.format("%s[%d].name", var, i - 1)) })
         end
      end
      inventory_category_scan("items", personal_inv_list_data)
      inventory_category_scan("weapons", personal_inv_list_data)
      inventory_category_scan("potions", personal_inv_list_data)
	  for i = 1, #personal_inv_list_data do
         wesnoth.set_dialog_value(personal_inv_list_data[i].image, "inventory_list_personal", i, "list_image")
         wesnoth.set_dialog_value(personal_inv_list_data[i].name, "inventory_list_personal", i, "list_name")
	  end

      local u_id = "shared"
	  local shared_inv_list_data = {}
      inventory_category_scan("items", shared_inv_list_data)
      inventory_category_scan("weapons", shared_inv_list_data)
      inventory_category_scan("potions", shared_inv_list_data)
	  for i = 1, #shared_inv_list_data do
         wesnoth.set_dialog_value(shared_inv_list_data[i].image, "inventory_list_shared", i, "list_image")
         wesnoth.set_dialog_value(shared_inv_list_data[i].name, "inventory_list_shared", i, "list_name")
	  end

      local function select_from_inventory(list, data)
         local i = wesnoth.get_dialog_value(list)

         wesnoth.set_dialog_value(data[i].image, "details_image")
         wesnoth.set_dialog_value(data[i].name, "details_name")
         -- insert any other things that need to be updated here
      end

      wesnoth.set_dialog_callback(select_from_inventory("inventory_list_personal", personal_inv_list_data), "inventory_list_personal")
      wesnoth.set_dialog_callback(select_from_inventory("inventory_list_shared", shared_inv_list_data), "inventory_list_shared")

      wesnoth.set_dialog_value("DESCRIPTION PLACEHOLDER", "details_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
(I also fixed an issue where you were overwriting item data from one category with item data from the next category, for example shared weapons being overwritten by shared potions.)
User avatar
vultraz
Developer
Posts: 960
Joined: February 7th, 2011, 12:51 pm
Location: Dodging Daleks

Re: vultraz's lua questions

Post by vultraz »

ummm....the for blocks seem to be using a var that doesn't have a number value or exist (unless I just didn't spot it). Also, what's with the #? eg:

Code: Select all

for i = 1, #shared_inv_list_data 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?
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 »

It's declared four lines above, as a local empty table, then stuff is added to it. # returns the number of characters in a string or entries in a table.
Exasperation
Posts: 462
Joined: June 8th, 2006, 3:25 am

Re: vultraz's lua questions

Post by Exasperation »

Specifically, #t is the highest number n such that for all i = 1, 2, .. n the statement t ~= nil is true (or 0 if t[1] == nil). So if the table t has entries with indices 1, 2, "a", "b", and 5, #t is 2. Since these particular tables were set up to only have entries with consecutive numerical indices starting at 1, # gives their length.
For a table representing a WML container, # will generally give the number of sub-containers (although you could probably confuse it if you had WML variables named things like "foo.2" ... I don't know what would happen if you tried to use wesnoth.get_variable on a WML container with both one or more sub-containers and a scalar member named 1).
User avatar
vultraz
Developer
Posts: 960
Joined: February 7th, 2011, 12:51 pm
Location: Dodging Daleks

Re: vultraz's lua questions

Post by vultraz »

Ahh found the problem.

Now for this next error....

apparently it hates this:

Code: Select all

local function select_from_inventory(list, data)
			local i = wesnoth.get_dialog_value(list)

			wesnoth.set_dialog_value(data[i].image, "details_image")
			wesnoth.set_dialog_value(data[i].name, "details_name")
		-- insert any other things that need to be updated here
		end
Picture 34.png
Maybe something to do with i also being substituted in the middle of another substitution?
Last edited by vultraz on January 3rd, 2012, 8:53 am, edited 1 time in total.
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 »

Hmm... try replacing

Code: Select all

      wesnoth.set_dialog_callback(select_from_inventory("inventory_list_personal", personal_inv_list_data), "inventory_list_personal")
      wesnoth.set_dialog_callback(select_from_inventory("inventory_list_shared", shared_inv_list_data), "inventory_list_shared")
with

Code: Select all

      wesnoth.set_dialog_callback(function() select_from_inventory("inventory_list_personal", personal_inv_list_data) end, "inventory_list_personal")
      wesnoth.set_dialog_callback(function() select_from_inventory("inventory_list_shared", shared_inv_list_data) end, "inventory_list_shared")
User avatar
vultraz
Developer
Posts: 960
Joined: February 7th, 2011, 12:51 pm
Location: Dodging Daleks

Re: vultraz's lua questions

Post by vultraz »

All right, displays now, but one small problem: listboxes don't appear :hmm: Doesn't even spit any errors :?

Code: Select all

-- This brings up the custom inventory control window
function wml_actions.inventory_controller(cfg)
	local item_list_personal = T.listbox { id = "inventory_list_personal",
				               definition = "default",
				               T.scrollbar { T.grid { T.row { T.column { T.vertical_scrollbar { } } } } },
				               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 = "list_checkbox",
																	             definition = "default",
																	             linked_group = "checkbox" } },
													                T.column { grow_factor = 1,
															           horizontal_grow = "true",
															           border = "all",
															           border_size = 5,
														                   T.image { id = "list_image",
																             definition = "default",
																             linked_group = "name" } },
													                T.column { grow_factor = 1,
															           horizontal_grow = "true",
															           border = "all",
															           border_size = 5,
															           T.label { id = "list_name",
																             definition = "default",
																             linked_group = "name" } } } } } } } } }

	local item_list_shared = T.listbox { id = "inventory_list_shared",
				             definition = "default",
				             T.scrollbar { T.grid { T.row { T.column { T.vertical_scrollbar { } } } } },
				             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 = "list_checkbox",
							    											   definition = "default",
							    											   linked_group = "checkbox" } },
			       											      T.column { grow_factor = 1,
				         										         horizontal_grow = "true",
					  											 border = "all",
					  											 border_size = 5,
					 											 T.image { id = "list_image",
					            										 	   definition = "default",
						    											   linked_group = "name" } },
			       											      T.column { grow_factor = 1,
					 											 horizontal_grow = "true",
					  											 border = "all",
					 											 border_size = 5,
					  											 T.label { id = "list_name",
						    											   definition = "default",
						    											   linked_group = "name" } } } } } } } } }

	local main_window_temp = { T.helptip { id="tooltip_large" }, -- mandatory field												
			      	   T.tooltip { id="tooltip_large" }, -- mandatory field
			           T.linked_group { id = "image",
						    fixed_height = "true",
						    fixed_width = "true" },
				   T.linked_group { id = "checkbox",
						    fixed_height = "true",
						    fixed_width = "true" },
				   T.linked_group { id = "name",
						    fixed_height = "true",
						    fixed_width = "false" },
			           height = 600,
			           width = 700,	
			           T.grid { T.row { T.column { item_list_personal },
						    T.column { item_list_shared } } } }

	
	local main_window = { T.helptip { id="tooltip_large" }, -- mandatory field												
			      T.tooltip { id="tooltip_large" }, -- mandatory field
			      T.linked_group { id = "image",
					       fixed_height = "true",
					       fixed_width = "true" },
			      T.linked_group { id = "checkbox",
				    	       fixed_height = "true",
					       fixed_width = "true" },
			      T.linked_group { id = "name",
					       fixed_height = "true",
					       fixed_width = "false" }, 
			      height = 600,
			      width = 700,	
			      T.grid { T.row { growth_factor = 1, 
					       T.column { grow_factor = 1,
						          horizontal_alignment = "left",
							  border = "all",
							  border_size = 5,
							  T.label { definition = "title", 
								    label = _"Inventory" } } },
				       T.row { T.column { T.grid { T.row { T.column { item_list_personal },
									   T.column { T.spacer { definition = default, width=10 } },
									   T.column { item_list_shared } } } } },
				       T.row { T.column { T.grid { T.row { T.column { vertical_alignment = "center",
							  			      horizontal_alignment = "center",
							  			      border = "all",
							  			      border_size = 5, 
							  			      T.image { id = "details_image" } },
					       				   T.column { vertical_alignment = "top",
							  			      horizontal_alignment = "center",
							  			      border = "all",
							  			      border_size = 5,
										      T.grid { T.row { T.column { T.label { definition = "title", id = "details_name" } } },
											       T.row { T.column { T.label { id = "details_description" } } } } },
					       				   T.column { T.grid { T.row { T.column { T.button { id = "switch_button", return_value = 3 } } }, 
								   		               T.row { T.column { T.button { id = "leave_chest_button", return_value = 2 } } },
								   		               T.row { T.column { T.button { id = "ok_button", return_value = 1 } } } } } } } } }		  
				      } }       
	
	local function item_preshow()
		local u_id = wesnoth.get_variable("unit.id")
		local personal_inv_list_data = {}
		local function inventory_category_scan(section, list)
			local var = u_id .. section
			for i = 1, wesnoth.get_variable(var .. ".length") do
				table.insert(list, { image = wesnoth.get_variable(string.format("%s[%d].image", var, i - 1)), name = wesnoth.get_variable(string.format("%s[%d].name", var, i - 1)) })
			end
		end

		inventory_category_scan("items", personal_inv_list_data)
		inventory_category_scan("weapons", personal_inv_list_data)
		inventory_category_scan("potions", personal_inv_list_data)
		for i = 1, #personal_inv_list_data do
			wesnoth.set_dialog_value(personal_inv_list_data[i].image, "inventory_list_personal", i, "list_image")
			wesnoth.set_dialog_value(personal_inv_list_data[i].name, "inventory_list_personal", i, "list_name")
		end

		local u_id = "shared"
		local shared_inv_list_data = {}
		inventory_category_scan("items", shared_inv_list_data)
		inventory_category_scan("weapons", shared_inv_list_data)
		inventory_category_scan("potions", shared_inv_list_data)
		for i = 1, #shared_inv_list_data do
			wesnoth.set_dialog_value(shared_inv_list_data[i].image, "inventory_list_shared", i, "list_image")
			wesnoth.set_dialog_value(shared_inv_list_data[i].name, "inventory_list_shared", i, "list_name")
     		end

		local function select_from_inventory(list, data)
			local i = wesnoth.get_dialog_value(list)

			wesnoth.set_dialog_value(data[i].image, "details_image")
			wesnoth.set_dialog_value(data[i].name, "details_name")
		-- insert any other things that need to be updated here
		end

		wesnoth.set_dialog_callback(function() select_from_inventory("inventory_list_personal", personal_inv_list_data) end, "inventory_list_personal")
		wesnoth.set_dialog_callback(function() select_from_inventory("inventory_list_shared", shared_inv_list_data) end, "inventory_list_shared")

		wesnoth.set_dialog_value("DESCRIPTION PLACEHOLDER", "details_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

	local function sync()
		local function item_postshow()
		end

		local return_value = wesnoth.show_dialog( main_window, item_preshow, item_postshow )

		return { return_value = return_value }
	end

	local return_table = wesnoth.synchronize_choice(sync)
end
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 »

Just a stupid mistake on my part (left another '.' out of a variable path). I also caught where you had declared u_id as local twice, causing it to load the personal values for both personal and shared.

Code: Select all

local function item_preshow()
      local u_id = wesnoth.get_variable("unit.id")
      local function inventory_category_scan(section, list)
         local var = string.format("%s.%s", u_id, section)
         for i = 1, wesnoth.get_variable(var .. ".length") do
            table.insert(list, { image = wesnoth.get_variable(string.format("%s[%d].image", var, i - 1)), name = wesnoth.get_variable(string.format("%s[%d].name", var, i - 1)) })
         end
      end

	  local personal_inv_list_data = {}
      inventory_category_scan("items", personal_inv_list_data)
      inventory_category_scan("weapons", personal_inv_list_data)
      inventory_category_scan("potions", personal_inv_list_data)
      for i = 1, #personal_inv_list_data do
         wesnoth.set_dialog_value(personal_inv_list_data[i].image, "inventory_list_personal", i, "list_image")
         wesnoth.set_dialog_value(personal_inv_list_data[i].name, "inventory_list_personal", i, "list_name")
      end

      u_id = "shared"
      local shared_inv_list_data = {}
      inventory_category_scan("items", shared_inv_list_data)
      inventory_category_scan("weapons", shared_inv_list_data)
      inventory_category_scan("potions", shared_inv_list_data)
      for i = 1, #shared_inv_list_data do
         wesnoth.set_dialog_value(shared_inv_list_data[i].image, "inventory_list_shared", i, "list_image")
         wesnoth.set_dialog_value(shared_inv_list_data[i].name, "inventory_list_shared", i, "list_name")
      end

      local function select_from_inventory(list, data)
         local i = wesnoth.get_dialog_value(list)

         wesnoth.set_dialog_value(data[i].image, "details_image")
         wesnoth.set_dialog_value(data[i].name, "details_name")
      -- insert any other things that need to be updated here
      end

      wesnoth.set_dialog_callback(function() select_from_inventory("inventory_list_personal", personal_inv_list_data) end, "inventory_list_personal")
      wesnoth.set_dialog_callback(function() select_from_inventory("inventory_list_shared", shared_inv_list_data) end, "inventory_list_shared")

      wesnoth.set_dialog_value("DESCRIPTION PLACEHOLDER", "details_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
User avatar
vultraz
Developer
Posts: 960
Joined: February 7th, 2011, 12:51 pm
Location: Dodging Daleks

Re: vultraz's lua questions

Post by vultraz »

Um....the details dialogues aren't appearing. The callback works, as when there was an error (with the description part) it spit errors. No it doesn't split anything...the image, name, and description just don't appear. Maybe something like another missing '.' but I can't see it, even after I examined the relative code. Sorry.

Code: Select all

	local main_window = { T.helptip { id="tooltip_large" }, -- mandatory field												
			      T.tooltip { id="tooltip_large" }, -- mandatory field
			      T.linked_group { id = "image",
					       fixed_height = "true",
					       fixed_width = "true" },
			      T.linked_group { id = "checkbox",
				    	       fixed_height = "true",
					       fixed_width = "true" },
			      T.linked_group { id = "name",
					       fixed_height = "true",
					       fixed_width = "false" }, 
			      height = 600,
			      width = 700,	
			      T.grid { T.row { growth_factor = 1, 
					       T.column { grow_factor = 1,
						          horizontal_alignment = "left",
							  border = "all",
							  border_size = 5,
							  T.label { definition = "title", label = _"Inventory" } } },
				       T.row { T.column { T.grid { T.row { T.column { item_list_personal },
									   T.column { T.spacer { definition = default, width=10 } },
									   T.column { item_list_shared } } } } },
				       T.row { T.column { T.grid { T.row { T.column { vertical_alignment = "center",
							  			      horizontal_alignment = "center",
							  			      border = "all",
							  			      border_size = 5, 
							  			      T.image { id = "details_image" } },
					       				   T.column { vertical_alignment = "top",
							  			      horizontal_alignment = "center",
							  			      border = "all",
							  			      border_size = 5,
										      T.grid { T.row { T.column { T.label { definition = "title", id = "details_name" } } },
											       T.row { T.column { T.label { id = "details_description" } } } } },
					       				   T.column { T.grid { T.row { T.column { T.button { id = "switch_button", return_value = 3 } } }, 
								   		               T.row { T.column { T.button { id = "leave_chest_button", return_value = 2 } } },
								   		               T.row { T.column { T.button { id = "ok_button", return_value = 1 } } } } } } } } }		  
				      } }       
	
	local function item_preshow()
		local u_id = wesnoth.get_variable("unit.id")
		local function inventory_category_scan(section, list)
			local var = string.format("%s.%s", u_id, section)
			for i = 1, wesnoth.get_variable(var .. ".length") do
				table.insert(list, { image = wesnoth.get_variable(string.format("%s[%d].image", var, i - 1)), 
						     name = wesnoth.get_variable(string.format("%s[%d].name", var, i - 1)),
						     description = wesnoth.get_variable(string.format("%s[%d].description", var, i - 1)) })
			end
		end

		-- Prints personal items list
		local personal_inv_list_data = {}
		inventory_category_scan("items", personal_inv_list_data)
		inventory_category_scan("weapons", personal_inv_list_data)
		inventory_category_scan("potions", personal_inv_list_data)
		for i = 1, #personal_inv_list_data do
			wesnoth.set_dialog_value(personal_inv_list_data[i].image, "inventory_list_personal", i, "list_image")
			wesnoth.set_dialog_value(personal_inv_list_data[i].name, "inventory_list_personal", i, "list_name")
		end

		-- Prints shared items list
		u_id = "shared"
		local shared_inv_list_data = {}
		inventory_category_scan("items", shared_inv_list_data)
		inventory_category_scan("weapons", shared_inv_list_data)
		inventory_category_scan("potions", shared_inv_list_data)
		for i = 1, #shared_inv_list_data do
			wesnoth.set_dialog_value(shared_inv_list_data[i].image, "inventory_list_shared", i, "list_image")
			wesnoth.set_dialog_value(shared_inv_list_data[i].name, "inventory_list_shared", i, "list_name")
		end

		-- Sets values for the details panel widgets
		local function select_from_inventory(list, data)
			local i = wesnoth.get_dialog_value(list)

			wesnoth.set_dialog_value(data[i].image, "details_image")
			wesnoth.set_dialog_value(data[i].name, "details_name")
			wesnoth.set_dialog_value(data[i].description, "details_description")
			-- insert any other things that need to be updated here
		end

		wesnoth.set_dialog_callback(function() select_from_inventory("inventory_list_personal", personal_inv_list_data) end, "inventory_list_personal")
		wesnoth.set_dialog_callback(function() select_from_inventory("inventory_list_shared", shared_inv_list_data) end, "inventory_list_shared")

		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
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 »

For details_image, just adding it to the image linked_group seems to be enough.
For the labels, it looks like you'll have to figure out how many characters they'll need to be able to hold, and fill them with a placeholder string containing that amount of whitespace. I expect that from the C++ interface it's possible to make a label re-evaluate its size, but from Lua it doesn't seem to be possible.
Post Reply