wesnoth.synchronize_selection

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

Moderator: Forum Moderators

Post Reply
User avatar
lipk
Posts: 637
Joined: July 18th, 2011, 1:42 pm

wesnoth.synchronize_selection

Post by lipk »

This works:

Code: Select all

      helper = wesnoth.require "lua/helper.lua"
	function f()
	  locations = helper.get_variable_array("some_array")
	  return locations
	end
      
      helper.set_variable_array("some_array", f())
This doesn't work (gives an empty array):

Code: Select all

      helper = wesnoth.require "lua/helper.lua"
      result = wesnoth.synchronize_choice(
	function ()
	  locations = helper.get_variable_array("some_array")
	  return locations
	end
      )
      
      helper.set_variable_array("some_array", result)
All on Wesnoth 1.10.5, either local or networked multiplayer.
Am I doing something wrong or is synchronize_selection broken?
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: wesnoth.synchronize_selection

Post by Elvish_Hunter »

Right now, I don't see anything wrong with the code. Are you attempting to use synchronize_choice in a prestart or start event? If yes, in past I noticed that such function does not work with these two events.
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
lipk
Posts: 637
Joined: July 18th, 2011, 1:42 pm

Re: wesnoth.synchronize_selection

Post by lipk »

The code is triggered by a menu item which itself is added in a start event. On an additional note, I could work around the problem by coding the contents of "some_array" into a string and returning a single-field table in the anonymous function.
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: wesnoth.synchronize_selection

Post by Anonymissimus »

synch_choice doesn't work earlier than turn 1, yes.

The return value of the inner function needs to be of type wml table. Yours should be of the form { a, b, c, ... } instead. (For finding out why some table isn't also a wml table, I suggest dbms from the WLP.)
You could use a modified helper function instead

Code: Select all

function helper.get_wml_variable_array(var)
	local result = {}
	for i = 1, wesnoth.get_variable(var .. ".length") do
		table. insert(result, { var, wesnoth.get_variable(string.format("%s[%d]", var, i - 1)) })
	end
	return result
end
and an analog helper.set_wml_variable_array(var) (untested), so that the form of the passed-through table should be a wml table
{ { "some_array", a }, { "some_array", b }, ... }
where a, b and so on are the assumed wml tables representing a single element/container in the wml variable array some_array.
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
lipk
Posts: 637
Joined: July 18th, 2011, 1:42 pm

Re: wesnoth.synchronize_selection

Post by lipk »

Ah-ha! I see now. Thankies.
Post Reply