diff --git a/data/campaigns/Eastern_Invasion/lua/bandits.lua b/data/campaigns/Eastern_Invasion/lua/bandits.lua index c77c5b3761e2..7c4d2fd48d05 100644 --- a/data/campaigns/Eastern_Invasion/lua/bandits.lua +++ b/data/campaigns/Eastern_Invasion/lua/bandits.lua @@ -35,7 +35,7 @@ end local function bandits_found(x,y) local bandit_types = vars.bandit_types - local bandit_villages = wml.variable.get_array("bandit_villages") + local bandit_villages = wml.array_access.get("bandit_villages") local boss_found = vars.boss_found local visited = vars.villages_visited local rand1 = helper.rand("3,4") @@ -69,7 +69,7 @@ local function bandits_found(x,y) end function wml_actions.bandit_village_capture(cfg) - local bandit_villages = wml.variable.get_proxy_array("bandit_villages") + local bandit_villages = wml.array_access.get_proxy("bandit_villages") local x = cfg.x or helper.wml_error("[bandit_village_capture] missing required x= attribute.") local y = cfg.y or helper.wml_error("[bandit_village_capture] missing required y= attribute.") diff --git a/data/lua/core.lua b/data/lua/core.lua index d1a3b1c2fb95..4e171ef3f4bc 100644 --- a/data/lua/core.lua +++ b/data/lua/core.lua @@ -213,11 +213,25 @@ end if wesnoth.kernel_type() == "Game Lua Kernel" then --[========[Basic variable access]========] - wml.variable = {} - wml.variable.get = wesnoth.get_variable - wml.variable.set = wesnoth.set_variable - wml.variable.get_all = wesnoth.get_all_vars + -- Get all variables via wml.all_variables (read-only) + setmetatable(wml, { + __metatable = "WML module", + __index = function(self, key) + if key == 'all_variables' then + return wesnoth.get_all_variables() + end + return rawget(self, key) + end, + __newindex = function(self, key, value) + if key == 'all_variables' then + error("all_variables is read-only") + -- TODO Implement writing? + end + rawset(self, key, value) + end + }) + -- Get and set variables via wml.variables[variable_path] wml.variables = setmetatable({}, { __metatable = "WML variables", __index = function(_, key) @@ -283,7 +297,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then end } - wml.variable.proxy = setmetatable({}, root_variable_mt) + wml.variables_proxy = setmetatable({}, root_variable_mt) --[========[Variable Array Access]========] @@ -314,9 +328,11 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then error(string.format("Invalid context for %s: expected nil, side, or unit", err_hint), 3) end + wml.array_access = {} + --! Fetches all the WML container variables with name @a var. --! @returns a table containing all the variables (starting at index 1). - function wml.variable.get_array(var, context) + function wml.array_access.get(var, context) context = resolve_variable_context(context, "get_variable_array") local result = {} for i = 1, context.get(var .. ".length") do @@ -326,7 +342,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then end --! Puts all the elements of table @a t inside a WML container with name @a var. - function wml.variable.set_array(var, t, context) + function wml.array_access.set(var, t, context) context = resolve_variable_context(context, "set_variable_array") context.set(var) for i, v in ipairs(t) do @@ -335,25 +351,36 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then end --! Creates proxies for all the WML container variables with name @a var. - --! This is similar to wml.variable.get_array, except that the elements + --! This is similar to wml.array_access.get, except that the elements --! can be used for writing too. --! @returns a table containing all the variable proxies (starting at index 1). - function wml.variable.get_proxy_array(var) + function wml.array_access.get_proxy(var) local result = {} for i = 1, wesnoth.get_variable(var .. ".length") do result[i] = get_variable_proxy(string.format("%s[%d]", var, i - 1)) end return result end + + -- More convenient when accessing global variables + wml.array_variables = setmetatable({}, { + __metatable = "WML variables", + __index = function(_, key) + return wml.array_access.get(key) + end, + __newindex = function(_, key, value) + wml.array_access.set(key, value) + end + }) end -- Some C++ functions are deprecated; apply the messages here. -- Note: It must happen AFTER the C++ functions are reassigned above to their new location. -- These deprecated functions will probably never be removed. if wesnoth.kernel_type() == "Game Lua Kernel" then - wesnoth.get_variable = wesnoth.deprecate_api('wesnoth.get_variable', 'wml.variable.get', 1, nil, wesnoth.get_variable) - wesnoth.set_variable = wesnoth.deprecate_api('wesnoth.set_variable', 'wml.variable.set', 1, nil, wesnoth.set_variable) - wesnoth.get_all_vars = wesnoth.deprecate_api('wesnoth.get_all_vars', 'wml.variable.get_all', 1, nil, wesnoth.get_all_vars) + wesnoth.get_variable = wesnoth.deprecate_api('wesnoth.get_variable', 'wml.variables', 1, nil, wesnoth.get_variable) + wesnoth.set_variable = wesnoth.deprecate_api('wesnoth.set_variable', 'wml.variables', 1, nil, wesnoth.set_variable) + wesnoth.get_all_vars = wesnoth.deprecate_api('wesnoth.get_all_vars', 'wml.all_variables', 1, nil, wesnoth.get_all_vars) end wesnoth.tovconfig = wesnoth.deprecate_api('wesnoth.tovconfig', 'wml.tovconfig', 1, nil, wesnoth.tovconfig) wesnoth.debug = wesnoth.deprecate_api('wesnoth.debug', 'wml.tostring', 1, nil, wesnoth.debug) diff --git a/data/lua/helper.lua b/data/lua/helper.lua index 4b0e32e6cca8..517203e2444a 100644 --- a/data/lua/helper.lua +++ b/data/lua/helper.lua @@ -99,11 +99,11 @@ function helper.move_unit_fake(filter, to_x, to_y) wesnoth.set_variable("LUA_move_unit") end --- Metatable that redirects access to wml.variable.proxy +-- Metatable that redirects access to wml.variables_proxy local proxy_var_mt = { __metatable = "WML variables", - __index = function(t, k) return wml.variable.proxy[k] end, - __newindex = function(t, k, v) wml.variable.proxy[k] = v end, + __index = function(t, k) return wml.variables_proxy[k] end, + __newindex = function(t, k, v) wml.variables_proxy[k] = v end, } function helper.set_wml_var_metatable(t) @@ -323,9 +323,9 @@ helper.child_count = wml.child_count helper.child_range = wml.child_range helper.child_array = wml.child_array if wesnoth.kernel_type() == "Game Lua Kernel" then - helper.get_variable_array = wml.variable.get_array - helper.set_variable_array = wml.variable.set_array - helper.get_variable_proxy_array = wml.variable.get_proxy_array + helper.get_variable_array = wml.array_access.get + helper.set_variable_array = wml.array_access.set + helper.get_variable_proxy_array = wml.array_access.get_proxy end helper.literal = wml.literal helper.parsed = wml.parsed diff --git a/data/lua/wml-flow.lua b/data/lua/wml-flow.lua index 5b86ca58a9a6..6c5ff49b9128 100644 --- a/data/lua/wml-flow.lua +++ b/data/lua/wml-flow.lua @@ -178,7 +178,7 @@ function wml_actions.foreach(cfg) end local array_name = cfg.array or helper.wml_error "[foreach] missing required array= attribute" - local array = wml.variable.get_array(array_name) + local array = wml.array_access.get(array_name) if #array == 0 then return end -- empty and scalars unwanted local item_name = cfg.variable or "this_item" local this_item = utils.start_var_scope(item_name) -- if this_item is already set @@ -232,7 +232,7 @@ function wml_actions.foreach(cfg) Note that altering the array via indexing (with the index_var) is not supported; any such changes will be reverted by this line. ]] - wml.variable.set_array(array_name, array) + wml.array_access.set(array_name, array) end function wml_actions.switch(cfg) diff --git a/data/lua/wml-utils.lua b/data/lua/wml-utils.lua index 520f3be29fe1..c329a1dd6260 100644 --- a/data/lua/wml-utils.lua +++ b/data/lua/wml-utils.lua @@ -192,7 +192,7 @@ end --note: when using these, make sure that nothing can throw over the call to end_var_scope function utils.start_var_scope(name) - local var = wml.variable.get_array(name) --containers and arrays + local var = wml.array_access.get(name) --containers and arrays if #var == 0 then var = wesnoth.get_variable(name) end --scalars (and nil/empty) wesnoth.set_variable(name) return var @@ -201,7 +201,7 @@ end function utils.end_var_scope(name, var) wesnoth.set_variable(name) if type(var) == "table" then - wml.variable.set_array(name, var) + wml.array_access.set(name, var) else wesnoth.set_variable(name, var) end diff --git a/data/lua/wml/set_variable.lua b/data/lua/wml/set_variable.lua index 203439a09311..ac62c49b5ba9 100644 --- a/data/lua/wml/set_variable.lua +++ b/data/lua/wml/set_variable.lua @@ -120,7 +120,7 @@ function wesnoth.wml_actions.set_variable(cfg) local string_to_join = '' - for i, element in ipairs(wml.variable.get_array(array_name)) do + for i, element in ipairs(wml.array_access.get(array_name)) do if element[key_name] ~= nil or (not remove_empty) then if #string_to_join > 0 then string_to_join = string_to_join .. separator