Thank you for answering. If I understand you, I should not use my enemy metatable for this, but only enemy scripts.
I also used the metatable for the hero: I created "hero:get_xp()" and "hero:set_xp(xp, boolean)" functions .(Maybe I will also create "hero:add_xp(xp)" ). It works fine, but I had then added a value in the backup file, otherwise I lost the xp value when I backed up and left the game. It would be easier without metatables for backups and to avoid having a value that is outdated.
I have another question:
Is it better to often read a value inside a metatable or a value from a backup file?
I put this code in the quest_manager.lua, maybe it is not the best file.
EDIT: I said "backup file" (for save1.dat, save2.dat or save3.dat), but I should have said "buffer"; e.g "xp = 100" is first read in a "backup file" and then the value is in the buffer of the engine. But with my method, I have xp value in the buffer (with other values found in the saveX.dat) + in the metatable which also used memory: I think my method is stupid, and I should avoid to insert data in the metatable.
I also used the metatable for the hero: I created "hero:get_xp()" and "hero:set_xp(xp, boolean)" functions .(Maybe I will also create "hero:add_xp(xp)" ). It works fine, but I had then added a value in the backup file, otherwise I lost the xp value when I backed up and left the game. It would be easier without metatables for backups and to avoid having a value that is outdated.
I have another question:
Is it better to often read a value inside a metatable or a value from a backup file?
I put this code in the quest_manager.lua, maybe it is not the best file.
Code (Lua) Select
-- Initializes some behavior of our hero.
local function initialize_hero()
local hero_meta = sol.main.get_metatable("hero")
-- ZL -- Functions for experience points of our hero
hero_meta.t_xp = {k_xp = nil}
function hero_meta:set_xp(v_xp, bool)
local game = self:get_game()
v_xp = v_xp or nil
bool = bool or false -- false means "do not save" with a set_value.
if type(v_xp) == "number" and type(bool) == "boolean" or bool == nil then
hero_meta.t_xp.k_xp = v_xp
if bool == true then
game:set_value("xp", v_xp)
end
else
print("error in set_xp(v_xp, bool)")
end
end
function hero_meta:get_xp()
local game = self:get_game()
local xp = game:get_value("xp")
if hero_meta.t_xp.k_xp == nil then
local hero = self:get_map():get_hero()
-- print("xp from file = ", xp)
hero:set_xp(xp) -- xp value is now in the metatable
return xp
else
-- print("xp from metatable = ", hero_meta.t_xp.k_xp)
return hero_meta.t_xp.k_xp
end
end
end
function quest_manager:initialize_quest()
-- blablabla
initialize_hero()
-- blablabla
end
EDIT: I said "backup file" (for save1.dat, save2.dat or save3.dat), but I should have said "buffer"; e.g "xp = 100" is first read in a "backup file" and then the value is in the buffer of the engine. But with my method, I have xp value in the buffer (with other values found in the saveX.dat) + in the metatable which also used memory: I think my method is stupid, and I should avoid to insert data in the metatable.