Hello, while I was doing my homework I got an idea from a simple algorithm I solved in my head (I get really bored making homework), I coded it, put it to test and everything works fine. Since I won't use it for my game, I'll share it here in case someone else who is new on programming (like me) or just too lazy wants to add a leveling system to their game, here it is:
-- Leveling up System by Miguelink 64.
-- First make a leveling up item on your items folder, I used a +2 to max hp for every level up.
-- heart_container
local item = ...
local game = item:get_game()
function item:on_obtaining()
game:add_max_life(2)
game:set_life(game:get_max_life())
end
-- Add to your dialogs a _treasure dialog for the item, mine says "Level up!".
-- You must copy this script to your enemies you want to give you exp.
local exp = 1 -- Change this to the wanted experience given by killing the enemy.
function enemy:on_dead()
if game:get_value("level") ~= game:get_value("max_level") then
game:set_value("experience",game:get_value("experience") + exp)
if game:get_value("experience") >= game:get_value("max_experience") then -- Checks if you can level up
hero:start_treasure("heart_container") -- You can change this for your leveling up item.
game:set_value("max_experience",game:get_value("max_experience") * 2) -- Doubles the amount needed to level up next time.
game:set_value("experience",0) -- Resets experience
game:set_value("level",game:get_value("level") + 1) -- Level up
end
end
end
-- You must copy this script to your initial game script under
-- function initial_game:initialize_new_game(game)
game:set_value("max_experience",50) -- Max experience needed to level up from level 1 to level 2
game:set_value("experience",0) -- Creates experience
game:set_value("max_level",20) -- Max level achievable
game:set_value("level",1) -- Start at level 1
Welp, have fün.