Skip to content

Commit

Permalink
Lua API: Some tweaks of the experimental wml module
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Mar 17, 2018
1 parent 893f71b commit de743f3
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions data/lua/core.lua
Expand Up @@ -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)
Expand Down Expand Up @@ -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]========]

Expand Down Expand Up @@ -316,7 +330,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then

--! 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
Expand All @@ -326,7 +340,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
Expand All @@ -338,13 +352,24 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
--! This is similar to wml.variable.get_array, 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.
Expand Down

0 comments on commit de743f3

Please sign in to comment.