I'm trying to get an XP system working and for testing I want to be able to either set the players level, or give them XP and they will level up that way, so for example if they need 1 xp to level up, and I give them 40 XP, then they will automatically be at level 40, and this somewhat works however as I have it now, the max xp I can give to get to level 100 is 30,000, however if i give the player 29999 xp, they are still at level 100, why is that? I cant seem to figure out why that is. I posted the main snipped of where the issues are, but the project has too many lines of code to post entirely, but you can download the VS 2019 project here: http://www.mediafire.com/file/bfdcv8tqru9j3z1/Monster_Fight.7z/file
I ran through it with the debugger, but I still cant seem to figure out what's going on. Its a logical error somewhere and i cant seem to figure out where.
GiveExperience:
Lets say that mExperience == amount == mMaxLevel * xpMultiplier
Therefore, amount <= mMaxLevel * xpMultiplier
and after function
mExperience == 2 * mMaxLevel * xpMultiplier
Don't you want to enforce mExperience <= mMaxLevel * xpMultiplier?
The first amount you apply:
1 2
test.LevelUp();
test.GiveExperience(amount);
All further amounts you apply:
1 2
test.GiveExperience(amount);
test.LevelUp();
Why the difference?
SetLevel:
You only set the level. Is it on purpose? Do you want that you can start a character at level N, but it has to collect same amount of experience as level 0 to reach level N+1?
Oops, the two functions not being in order was an accident, I fixed it. As for set level, its just there so i can set level to whatever i want for testing purposes, I just set it to 1 for this specific test.
Doing these kinds of checks in code really confuses me, its hard for me to follow for some reason even when the logic is perfect, i dont know why.
I still cant seem to get it to work, it still sets player to level 100 when i have 29,999 xp.
Yes, you get level 100, because you have more tha 29'700.
On the other hand: mExperience / xpMultiplier == 29'999 / 300 == 99
You can thus calculate level from experience without a loop (because each level requires 300 exp).
One more question. Now this code sets the experience and level to 30,000 and 100 respectively, but I was thinking maybe a better system would be to get the overflow and find a way to subtract from it to add the right amount of max level or max xp.
so for example if the players current level was 43 and i added 256 levels, the code would do:
mCurrentLevel = 256 - (100 - 43) + 143); //which is 56, then add 1?
Not sure that math is 100% correct, its missing 1 level but something like that. It took me longer than i'd like to admit just to come up with that formula.
I'm not sure if that way is better or if just setting it to the max level if it goes over is better or if it doesn't matter at all and is dependent upon what i need.