HUD - ZL - level (LVL) & experience (XP)
I try to create a game system with points of experience and levels for my Zelda-like project.
Starlock sent me an email: "I was also planning on making an xp system and was hoping I could see your code and fiddle around with it

"
So I share my work if this could be useful for others...
____________________
SCRIPTHere is the main script for the HUD. See in attachment.Put this script in '../data/scripts/hud/lvl_and_xp.lua'
In this script there is a table:
self.t_xp_to_levelup = {100, 200, 300, 400, 500, 600, 700, 800}
You are level 1 and you need 100 xp to level up.
You are level 2 and you need 200 xp to level up.
You are level 3 and you need 300 xp to level up.
...
You are level 8 and you need 800 xp to level up.
Then level 9 appears and xp counters are blocked (800/800). They appears in green because I used 'small_green_digits' font that I think you have got.
Put your own values. Obviously, it will not be 100, 200, 300 etc..

You are not limited to 8 values.
____________________
IMPORTANTYou will have to add some lines in certain lua files. See below.
I hope I don't forget something.

____________________
IMAGECopy this image. See in attachment.../data/sprites/hud/lvl_and_xp_icon.png
_________________
POSITION IN THE HUDIn '../data/scripts/hud.lua' , you need declare the position of lvl and xp elements.
(-104, 16) is an example.
---- Create each element of the HUD.
-- blablabla
local lvl_and_xp_builder = require("scripts/hud/lvl_and_xp")
-- blablabla
menu = lvl_and_xp_builder:new(game)
menu:set_dst_position(-104, 16)
hud.elements[#hud.elements + 1] = menu
-- blablabla
____________________
SAVEGAME FILEIn "../data/scripts/game_manager.lua", and more precisely in the "function game_manager:create(file)", we have to declare two values for a new savegame file. Also add 'current_level = 0' and 'current_xp = 0' in your existing savegame files.
-- if not exists then
---- This is a new savegame file.
-- blablabla
game:set_value("current_level", 0)
game:set_value("current_xp", 0)
-- end
-- blablabla
____________________
FUNCTIONS TO ADD, SET or GET XP and LEVELIn '../data/scripts/quest_manager.lua', because I think it is practical to add or set or get xp and level of our hero, I added some functions.
All these functions are not necessaries, but they are very useful in debug console to test.
hero:set_level(3) is equal to game:set_value("current_level", 3)
hero:add_level(1) so 'current_level = 4' in your 'saveX.dat' if you save the game.
hero:get_level() is equal to game:get_value("current_level")
hero:set_xp(100) is equal to game:set_value("current_xp", 100)
hero:add_xp(10) so 'current_xp = 110' in your 'saveX.dat' if you save the game. hero:add_xp(number) is the most useful for enemies.
hero:get_xp() is equal to game:get_value("current_xp")
-- Initializes some behavior of our hero.
local function initialize_hero()
local hero_meta = sol.main.get_metatable("hero")
-- ZL -- Functions for levels of our hero
function hero_meta:set_level(lvl)
if type(lvl) == "number" then
local game = self:get_game()
game:set_value("current_level", lvl)
else
print("Error in hero:set_level(lvl), lvl should be a number.")
end
end
function hero_meta:add_level(lvl)
if type(lvl) == "number" then
local game = self:get_game()
local current_level = game:get_value("current_level")
game:set_value("current_level", current_level + lvl)
else
print("Error in hero:add_level(lvl), lvl should be a number.")
end
end
function hero_meta:get_level()
local game = self:get_game()
local current_level = game:get_value("current_level")
return current_level
end
-- ZL -- Functions for experience points of our hero
function hero_meta:set_xp(xp)
if type(xp) == "number" then
local game = self:get_game()
game:set_value("current_xp", xp)
else
print("Error in hero:set_xp(xp), xp should be a number.")
end
end
function hero_meta:add_xp(xp)
if type(xp) == "number" then
local game = self:get_game()
local current_xp = game:get_value("current_xp")
game:set_value("current_xp", current_xp + xp)
else
print("Error in hero:add_xp(xp), xp should be a number.")
end
end
function hero_meta:get_xp()
local game = self:get_game()
local current_xp = game:get_value("current_xp")
return current_xp
end
end
Edit: Sorry, don't forget to initialize_hero() !!!
function quest_manager:initialize_quest()
-- blablabla
initialize_hero()
-- blablabla
end
____________________
ENEMIESIn your scripts for enemies where you have declared their behavior (e.g ../data/enemies/lib/slime_behavior.lua),
just add a variable named for example "properties.xp" in your 'function behavior:create(enemy, properties)'. I don't know, but the name of your function is perhaps different.
In
my example, I decided to define xp according to life, damage and speed of the enemy.
properties.xp = properties.xp or (properties.life * properties.damage * properties.normal_speed)
In this example, I added xp in the 'function enemy:on_dying()', I think it is logical, but you can do differently:
function enemy:on_dying()
local hero = enemy:get_map():get_entity("hero")
hero:add_xp(properties.xp)
end