Skip to content

Commit

Permalink
Add a more convenient way of registering custom saved game data
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Mar 17, 2018
1 parent ea410b1 commit 1f2d131
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions data/lua/core.lua
Expand Up @@ -394,6 +394,31 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
wml.array_access.set(key, value)
end
})

wesnoth.persistent_tags = {}

-- Note: We don't save the old on_load and on_save here.
-- It's not necessary because we know this will be the first one registered.
function wesnoth.game_events.on_load(cfg)
for i = 1, #cfg do
local tag = wesnoth.persistent_tags[cfg[i][1]]
if type(tag) == 'table' and type(tag.read) == 'function' then
tag.read(cfg)
end
end
end

function wesnoth.game_events.on_save()
local data_to_save = {}
for name, tag in pairs(wesnoth.persistent_tags) do
if type(tag) == 'table' and type(tag.write) == 'function' then
local function add(data)
table.insert(data_to_save, wml.tag[name](data))
end
tag.write(add)
end
end
end
end

-- Some C++ functions are deprecated; apply the messages here.
Expand Down

0 comments on commit 1f2d131

Please sign in to comment.