Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

level up formula

so i am making a skills system like that of oblivion. its pretty much done now. but i noticed if you add enough exp to go up more then one level it messes up my math. it says stuff like -35 to go up a level. so i was hoping some one would help me figure out how to get it to check if the player will go up more then one level. here is what i am using atm.(i retyped what i remember, might not be exact code, but close enough i think. dont know how much you need to know.)
need to know info:
$data_skill[skill].total is total exp in skill
$data_skill[skill].current is the current exp
$data_skill[skill].next is the amount of exp needed(current) to go up a level


Code:
 

def self.add_exp(skill, num)

       $data_skill[skill].total = $data_skill[skill].total + num

       $data_skill[skill].current = $data_skill[skill].current + num

       if $data_skill[skill].current >= $data_skill[skill].next

          $data_skill[skill].level = $data_skill[skill].level + 1

          change = $data_skill[skill].next - $data_skill[skill].current

            if change < 0

                change = change * -1

                $data_skill[skill].current = change

            elsif

                $data_skill[skill].current = 0

            end

       end

       if $data_skill[skill].level <= 24

         $data_skill[skill].next =$data_skill[skill].next +5

       end

       if $data_skill[skill].level > 24 && $data_skill[skill].level < 50

         $data_skill[skill].next =$data_skill[skill].next +10

       end

       if $data_skill[skill].level > 49 && $data_skill[skill].level < 74

         $data_skill[skill].next =$data_skill[skill].next +15

       end

       if $data_skill[skill].level > 74

         $data_skill[skill].next =$data_skill[skill].next +20

       end

end               

 

edit: think i could make an array containing all of the exp(total) required to go up a level then just have it check which two array elements the current $data_skill[skill].total is between?

like:

exp[1] = 5
exp[2] = 10
exp[3] = 15
exp[4] = 20
exp[5] = 25
im not sure how the check would look though...
 

Seto

Member

Hey, I would love to help. But could you explain exactly what you are trying to do? I don't know what the oblivion skill system is like, so I don't know what you are trying to accomplish .

When you said this,
Code:
 

need to know info:

$data_skill[skill].total is total exp in skill

$data_skill[skill].current is the current exp

$data_skill[skill].next is the amount of exp needed(current) to go up a level

 

What does $data_skill[skill].total actually hold? The maximum amount of exp possible for the skill (Like a total of all the levels?)

I don't know if this fits you plan or is helpful but I would do it like this,
create a multidimensional array, 2D Array. e.g. $data_skills[][]
then have the first array as the skills list, e.g. 0 = Magic, 1 = Healing ect...
and have the second as the amount of exp needed per level
so $data_skills[0][1] = the amount of exp needed to level up from Magic (0) Level 1 (1)
and so that you can keep calling the method with a skill name rather then a silly number like 0
have another array with the names of the skills in order $data_skill_list[]
so you give it the input "Magic" and it runs a loop
Code:
 

i = 0

#Loops till all skills are checked

while i < $data_skills_list.length

#If your input matches the name of one of the skills, use the index as skill_number so you can get data from #$game_skills[][]

if skill == $data_skills_list[i]

skill_number = i

end

i = i + 1

end

 

So once you have skill_number you can go
$data_skills[skill_number][level] and you get the amount of exp needed for the next level for that skill.
you would need something holding the current level of course like $data_skill_level[skill_number] and exp the same way $data_skill_exp[skill_number]
so you can do this after running the above loop
Code:
 

level = $data_skill_level[skill_number]

current_exp = $data_skill_exp[skill_number]

needed_exp = $data_skills[skill_number][level]

 

if current_exp >= needed_exp

$data_skill_level[skill_number] += 1

end

 

# Or you could do this, to check for multiple levels up. E.g. more exp then just one level was given

 

level = $data_skill_level[skill_number]

current_exp = $data_skill_exp[skill_number]

needed_exp = $data_skills[skill_number][level]

 

while current_exp >= needed_exp

$data_skill_level[skill_number] += 1

level = $data_skill_level[skill_number]

current_exp = $data_skill_exp[skill_number]

needed_exp = $data_skills[skill_number][level]

end

 
So there it checks if you have enough exp if you do it adds a level, and the second one checks if you have enough exp to go to the next level and if you do, it adds the level and check again till you don't have enough exp to level...

You would have to replace all my variables with your ones e.g. $data_skill[skill].current to the the current exp and what not.

Actually is that helpful at all? Sorry, I really don't know what your trying to get done. Plus I am not that great at scripting. Feel free to ignore me. If anything at all I said it helpful, it might be that while loop. You won't get errors like you said you were if the script already checks for multiple levels and adds them right?

Again sorry if I am on the wrong track! Sorry Sorry Sorry. If I did help, Yaywoo! Good luck either way. I am sure some-one else will be able to help you.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top