• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

(SOLVED)Total exp count not calculating correctly with large numbers.

flyinian

Member
I am currently developing a leveling system. So far, everything seems to work fine besides the total exp which calculates how much exp is gained on achieved levels(exp gain isn't calculated for not achieved levels).

The issue I am having is that the total exp(_SkillTotalExp) is calculated incorrectly once I surpass a certain amount. I suspect it may be the exp gain when it surpasses the exp(_Skill1EXPlvl) required to achieve next level if done in one go, it causes the issue and exponentially increases the total exp gained(_SkillTotalExp).

I've checked the levels(_Skill1) and those seems to be calculating correctly for the exp(_Skill1Exp) I give.


EX:

On the key press event I add a value of 9999(to _Skill1Exp). Then I go into the game and press the key, It will calculate the levels(_Skill1) fine, however, the total exp gain(__SkillTotalExp) will be significantly higher than 9999.



Create Event:
GML:
_Skill1EXP = 0;              //exp gained for the current level of the skill.
_Skill1EXPlvl = 10;        //exp required for next level.
_SkillTotalExp = 0;        //total gained exp for the skill/all skills.
_Skill1 = 1;                  //the level of the skill.

Step Event:
Code:
if(_Skill1EXP >= _Skill1EXPlvl)
{
    
    _Skill1 += 1;                                             //add 1 level to skill level
    
    _SkillTotalExp += _Skill1EXP;                    //Adds gained xp to total skill exp count
    
    _Skill1EXP = _Skill1EXP - _Skill1EXPlvl;       //allows exp to be added to next level without it being deleted
    
    _Skill1EXPlvl = 10 * _Skill1;                     //multiplies the required exp for next level gain.
};

Draw GUI Event:
Code:
draw_text(320, 140, "Skill 1 Level: "+string(_Skill1));    //Draws the level of the skill

draw_text(320, 150, "Skill 1 EXP to next level: "+string(_Skill1EXP) + " / " + string(_Skill1EXPlvl)); //draws the current gained exp for the current level out of the total exp needed for next level.

draw_text(320, 225, "Current Skill Level EXP Gained: "+string(_SkillTotalExp)); //draws the total exp gained from the skill/all skills.

Key Press - L:
Code:
_Skill1EXP += 10;     //adds 10 exp to the skill.

Thank you.
 

Nidoking

Member
_Skill1 += 1; //add 1 level to skill level
_SkillTotalExp += _Skill1EXP; //Adds gained xp to total skill exp count
_Skill1EXP = _Skill1EXP - _Skill1EXPlvl; //allows exp to be added to next level without it being deleted
Take a closer look at what you're doing in these three lines. You're adding the gained EXP to the total every step, but only reducing it by the number used to level up. Just update the total once, with the total gain.
 

flyinian

Member
Take a closer look at what you're doing in these three lines. You're adding the gained EXP to the total every step, but only reducing it by the number used to level up. Just update the total once, with the total gain.
How would I update the total once with the total gain? I understand with what you're saying, but I'm lost on how to implement it.

I could as well be lost with what you're saying.
 

LunaticEdit

Member
GML:
while (_Skill1EXP >= _Skill1EXPlvl) // <-- support multiple levels per tick
{  
    _Skill1++;
    _SkillTotalExp += _Skill1EXPlvl; // <-- This is where you went wrong
    _Skill1EXP     -= _Skill1EXPlvl;
    _Skill1EXPlvl   = 10 * _Skill1;
};
Not bashing, but try to keep your code as clean and small as possible, it makes seeing the problem much easier!
 

flyinian

Member
GML:
while (_Skill1EXP >= _Skill1EXPlvl) // <-- support multiple levels per tick
{ 
    _Skill1++;
    _SkillTotalExp += _Skill1EXPlvl; // <-- This is where you went wrong
    _Skill1EXP     -= _Skill1EXPlvl;
    _Skill1EXPlvl   = 10 * _Skill1;
};
Not bashing, but try to keep your code as clean and small as possible, it makes seeing the problem much easier!
Thank you, I think this has fixed my issue. I appreciate it.

I will definitely try and keep my code in better standing.
 

flyinian

Member
Take a closer look at what you're doing in these three lines. You're adding the gained EXP to the total every step, but only reducing it by the number used to level up. Just update the total once, with the total gain.
Thank you for your help.
 
Top