I'm really at a loss. I've sat here for hours trying to figure out what's wrong but I just can't see it.
So I have an array that holds integers, named xpData[]. It's used to keep track of how much XP is needed to "level up". It's a member variable of a class Player. I utilize this within a member function of that same class named AddXP().
Within said function, I add the function argument to the member variable xp. This works perfectly fine. It even clamps when hitting the maxExp integer. I've tried the function with just these statements as well, and the problem was "solved" (although it created another problem).
The problem is that I go on to, within the same function, utilize a while loop that checks the xp variable against the xpData[lvl] element. Whenever the xp of the Player is greater than or equal to xpData[lvl], it increments the lvl variable. The issue is that there are a limited number of levels. The maximum being 30, and the minimum 1. So I utilize a clamping function. Below is the code:
GeneralFunctions.cpp
int ClampInt(int n, int lower, int upper)
{
int updatedNum = n;
if (n <= lower)
updatedNum = lower;
else if (n >= upper)
updatedNum = upper;
return updatedNum;
}
Player.h
class Player
{
public:
Player();
void AddXP(int amount);
int GetLevel() const { return lvl; };
int GetXP() const { return xp; };
private:
int minLevel;
int maxLevel;
int lvl;
int maxExp;
int xp;
int xpData[31] = {
0, // zero-based
10, // Level 1
20, // Level 2
30, // Level 3
40, // Level 4
50, // Level 5
60, // Level 6
70, // Level 7
80, // Level 8
90, // Level 9
100, // Level 10
110, // Level 11
120, // Level 12
130, // Level 13
140, // Level 14
150, // Level 15
160, // Level 16
170, // Level 17
180, // Level 18
190, // Level 19
200, // Level 20
210, // Level 21
220, // Level 22
230, // Level 23
240, // Level 24
250, // Level 25
260, // Level 26
270, // Level 27
280, // Level 28
290, // Level 29
300 // Level 30
};
};
Player.cpp
Player::Player()
{
minLevel = 1;
maxLevel = 30;
lvl = 1;
maxExp = 1000000;
xp = 0;
}
void Player::AddXP(int amount)
{
if (xp + amount > maxExp)
xp += (maxExp - xp);
else
xp += amount;
while (xp >= xpData[lvl])
{
lvl = ClampInt(lvl + 1, minLevel, maxLevel);
}
}
The issue is that when the xp reaches above the xp required for "level 30", the program just kinda... freezes. No errors, no crashes, nothing. Just freezing. I assume is has something to do with the loop obviously, but I have no clue what to do to fix it, without giving up much needed functionality (ie clamping the levels, or checking within a loop in case the xp added is ever enough to level the player up multiple times, etc.).
Thanks for any help!
Within the loop
while (xp >= xpData[lvl])
{
lvl = ClampInt(lvl + 1, minLevel, maxLevel);
}
the ClampInt function limits lvl to 30 and the while condition keeps beeing satisfied so it loops forever.
Maybe you should just get rid of the ClampInt function and simply use
while(lvl < 30 && xp >= xpData[lvl])
++lvl;
Or you could define a level 31 with an XP of INT_MAX which should not be reached
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.