Author Topic: Simple Leveling up system  (Read 11430 times)

0 Members and 1 Guest are viewing this topic.

Miguelink 64
  • Newbie
  • *
  • Posts: 8
    • View Profile
Simple Leveling up system
« on: March 05, 2019, 05:31:12 AM »
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:


Code: [Select]
-- 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.
« Last Edit: March 05, 2019, 05:37:10 AM by Miguelink 64 »

froggy77

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: Simple Leveling up system
« Reply #1 on: March 06, 2019, 10:37:47 PM »
It reminds me of a script I made, but yours is at least up to date.
There have been a lot of script sharing recently. Thank you to you and the Solarus community. It's cool.

Diarandor

  • Hero Member
  • *****
  • Posts: 1062
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Simple Leveling up system
« Reply #2 on: March 07, 2019, 10:51:35 PM »
I recommend to use a linear growth instead of an exponential one. You would need only 26214400 xp to level up from Lv19 to Lv20, heheh.

https://en.m.wikipedia.org/wiki/Wheat_and_chessboard_problem
« Last Edit: March 07, 2019, 10:53:24 PM by Diarandor »
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Miguelink 64
  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Simple Leveling up system
« Reply #3 on: March 08, 2019, 04:51:19 PM »
I didn't knew the exact amount needed to get that level, linear growing could be a good solution, but exponential is one most RPGs uses, since Lua only accepts intergers, that's the lowest I could get without getting too arithmetic to make a proper exponential growth,  thanks

YoshiMario2000
  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Re: Simple Leveling up system
« Reply #4 on: March 09, 2019, 01:29:48 AM »
Since when did lua only accept integers?
I've done quite a bit of programming in lua where I've used non-integer numbers and the functions math.ceil() and math.floor() exist, which round a number up or down respectively.
This signature was way too long before, but now it's short!
Also, I am Still Alive!
On ad Off I go!

Do you ever get the feeling that the fandom of a product(s) ruin the potential that you could have had to enjoy the product?

Max

  • Sr. Member
  • ****
  • Posts: 276
    • View Profile
Re: Simple Leveling up system
« Reply #5 on: March 09, 2019, 06:08:27 AM »
Lua is all cool with decimals, but I believe game:set_value() will round to the nearest integer, as will most engine functions like set_life.

Miguelink 64
  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Simple Leveling up system
« Reply #6 on: March 12, 2019, 07:11:10 PM »
I haven't read the whole API for LUA, that's why I thought it only accepted intergers, thanks for the feedback  ;D

Mr Shiek

  • Newbie
  • *
  • Posts: 29
  • Game Design 'Hobbyist'
    • View Profile
Re: Simple Leveling up system
« Reply #7 on: July 19, 2019, 12:38:14 AM »
Lua is all cool with decimals, but I believe game:set_value() will round to the nearest integer, as will most engine functions like set_life.

sorry for the necro, but a relatively easy way around this is to convert to and from a string when saving the value and getting the value. game functions won't remove the decimal if the value is saved as a string.
Build a man a fire, he will be warm for the night. Set a man on fire, he will be warm for the rest of his life.
a.k.a. Kamigousu, Xejk, Sr Xejk