Getting player level from total XP without a loop

I didn’t know it was just a matter of balancing equations. This is actually just what I was looking for, thank you for that.

A tiny thing I noticed, you need to add 1 to experience, else it will be behind one. For example in my formula for level, for level 2, you need to get 35 XP.

Ex.

level = 2
math.floor(100 * (((level - 1) * 0.5) ^ 1.5)) --> 35

experience = 35
floor(2*(experience/100)^(2/3) + 1) --> 1
floor(2*((experience + 1)/100)^(2/3) + 1) --> 2
1 Like

It’s mostly because “:” is used for classes are created and “.” just for re-usable blocks of code. Also calling a common function (used many times) with “:” is actually less efficient because you need shift.

Summarized: It triggers something deep within me

yea that’s due to floating point errors… I noticed when I tried another levels experience that was a decimal. Good catch! :+1:

1 Like

I wonder if that’s accurate though. Because wouldn’t that XPForLevel method be returning the XP required simply to gain the next level? Not the total XP require for the current level?

making the equation actually:

level = 2*(xpReqForLevel/100)^(2/3) + 1

he would be then passing in totalXP, giving an inaccurate calculation.

1 Like

I might be misunderstanding your question (probably because of my awful naming conventions) but it does work, behaviour is identical to how it was while I was using a loop. I think the problem you brought up is solved by adding 1 to experience then flooring it

Well you did say that you weren’t zeroing the XP for each level, so perhaps that is why it is working. Of course that is also probably why your players are leveling up so quickly. But if it works for your project then good deal. :grin:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.