HUD: ZL - level (LVL) & experience (XP)

Started by froggy77, January 28, 2016, 06:54:26 PM

Previous topic - Next topic
January 28, 2016, 06:54:26 PM Last Edit: January 28, 2016, 10:31:48 PM by froggy77
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 :P"
So I share my work if this could be useful for others...

____________________

SCRIPT

Here 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.

____________________

IMPORTANT

You will have to add some lines in certain lua files. See below.
I hope I don't forget something.  ;D

____________________

IMAGE

Copy this image. See in attachment.

../data/sprites/hud/lvl_and_xp_icon.png

_________________

POSITION IN THE HUD

In '../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 FILE

In "../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 LEVEL

In '../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")


Code (lua) Select

-- 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() !!!
Code (Lua) Select

function quest_manager:initialize_quest()

  -- blablabla
  initialize_hero()
  -- blablabla

end


____________________

ENEMIES

In 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

Fantastic, absolutely superb, nice work.

This should be pinned if possible.

January 28, 2016, 10:16:15 PM #2 Last Edit: January 28, 2016, 10:42:41 PM by froggy77
Thanks Metalzelda  :)
If someone has tested this xp and level system, please tell me if my tutorial is clear and if it works correctly in your game.

I will update the first post as I just do it for initialize_hero().

Thanks frog! I'll test it out on my game as soon as I get the chance. :D

Sorry for the necropost. It is not my fault; it's Miguelink 64's fault  ;D and its topic named "Simple Leveling up system"

Yeury already sent me an email in 2017 telling me that my script was not up to date for the part "quest_manager.lua".
I did not understand thinking that "quest_manager.lua" was the standard in Solarus scripts. I was completely wrong and I realize that I should review the script, but I would have to go back to the lua.I had a little level to do something nice enough, but I'm rusty today. I do not understand anymore my script (or at least some parts) and I do not even know or put the content of "quest_manager.lua" to be at the current standard of Solarus scripts. I would like a little help.
Thanks in advance.