Move the Lua global variable helpers from WC to a new "experimental" module
This commit is contained in:
parent
f4d528f616
commit
1205e3fd25
2 changed files with 55 additions and 39 deletions
|
@ -121,43 +121,7 @@ function wc2_utils.has_no_advances(u)
|
|||
return #u.advances_to == 0
|
||||
end
|
||||
|
||||
local global_vars = setmetatable({}, {
|
||||
__index = function(self, namespace)
|
||||
return setmetatable({}, {
|
||||
__index = function(self, name)
|
||||
wml.variables.lua_global_variable = nil
|
||||
wesnoth.unsynced(function()
|
||||
wesnoth.wml_actions.get_global_variable {
|
||||
namespace = namespace,
|
||||
to_local = "lua_global_variable",
|
||||
from_global = name,
|
||||
immediate = true,
|
||||
}
|
||||
end)
|
||||
local res = wml.variables.lua_global_variable
|
||||
wml.variables.lua_global_variable = nil
|
||||
if res == "" then
|
||||
return nil
|
||||
end
|
||||
return res
|
||||
end,
|
||||
__newindex = function(self, name, val)
|
||||
wml.variables.lua_global_variable = val
|
||||
wesnoth.unsynced(function()
|
||||
wesnoth.wml_actions.set_global_variable {
|
||||
namespace = namespace,
|
||||
from_local = "lua_global_variable",
|
||||
to_global = name,
|
||||
immediate = true,
|
||||
}
|
||||
end)
|
||||
wml.variables.lua_global_variable = nil
|
||||
end,
|
||||
})
|
||||
end
|
||||
})
|
||||
|
||||
wc2_utils.global_vars = global_vars.wc2
|
||||
wc2_utils.global_vars = wesnoth.experimental.wml.global_vars.wc2
|
||||
|
||||
if rawget(_G, "wc2_menu_filters") == nil then
|
||||
wc2_menu_filters = {}
|
||||
|
|
|
@ -294,8 +294,11 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
|
|||
elseif type(ctx) == 'number' and ctx > 0 and ctx <= #wesnoth.sides then
|
||||
return resolve_variable_context(wesnoth.sides[ctx])
|
||||
elseif type(ctx) == 'string' then
|
||||
-- TODO: Treat it as a namespace for a global (persistent) variable
|
||||
-- (Need Lua API for accessing them first, though.)
|
||||
-- Treat it as a namespace for a global (persistent) variable
|
||||
return {
|
||||
get = function(path) return wesnoth.experimental.wml.global_vars[ctx][path] end,
|
||||
set = function(path, val) wesnoth.experimental.wml.global_vars[ctx][path] = val end,
|
||||
}
|
||||
elseif getmetatable(ctx) == "unit" then
|
||||
return {
|
||||
get = function(path) return ctx.variables[path] end,
|
||||
|
@ -312,6 +315,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
|
|||
set = function(path, val) ctx[path] = val end,
|
||||
}
|
||||
end
|
||||
-- TODO: Once the global variables API is no longer experimental, add it as a supported context type in this error message.
|
||||
error(string.format("Invalid context for %s: expected nil, side, or unit", err_hint), 3)
|
||||
end
|
||||
|
||||
|
@ -359,6 +363,54 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
|
|||
wml.array_access.set(key, value)
|
||||
end
|
||||
})
|
||||
|
||||
--[========[Global persistent variables]========]
|
||||
local ns_key, global_temp = '$ns$', "lua_global_variable"
|
||||
local global_vars_ns = {}
|
||||
local global_vars_mt = {
|
||||
__index = function(self, namespace)
|
||||
setmetatable({[ns_key] = namespace}, global_vars_ns)
|
||||
end
|
||||
}
|
||||
|
||||
function global_vars_ns.__index(self, name)
|
||||
local U = wesnoth.require "wml-utils"
|
||||
local var <close> = U.scoped_var(global_temp)
|
||||
wesnoth.unsynced(function()
|
||||
wesnoth.wml_actions.get_global_variable {
|
||||
namespace = self[ns_key],
|
||||
to_local = global_temp,
|
||||
from_global = name,
|
||||
immediate = true,
|
||||
}
|
||||
end)
|
||||
local res = var:get()
|
||||
if res == "" then
|
||||
return nil
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
function global_vars_ns.__newindex(self, name, val)
|
||||
local U = wesnoth.require "wml-utils"
|
||||
local var <close> = U.scoped_var(global_temp)
|
||||
var:set(val)
|
||||
wesnoth.unsynced(function()
|
||||
wesnoth.wml_actions.set_global_variable {
|
||||
namespace = self[ns_key],
|
||||
from_local = global_temp,
|
||||
to_global = name,
|
||||
immediate = true,
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
-- Make sure wesnoth.experimental.wml actually exists
|
||||
-- It's done this way so it doesn't break if we later need to add things here from C++
|
||||
wesnoth.experimental = wesnoth.experimental or {}
|
||||
wesnoth.experimental.wml = wesnoth.experimental.wml or {}
|
||||
|
||||
wesnoth.experimental.wml.global_vars = setmetatable({}, global_vars_mt)
|
||||
else
|
||||
--[========[Backwards compatibility for wml.tovconfig]========]
|
||||
local fake_vconfig_mt = {
|
||||
|
|
Loading…
Add table
Reference in a new issue