Sorry in advance for the long explanation of how the levels/experience is awarded. There’s a bunch of extraneous information but it gives insight as to why I didn’t consider certain routes.
The reason overall is because loops are relatively slow when compared to a simple formula.
We’re awarding XP based on the level the player is. The higher a player’s level, the more XP they’ll be awarded, so required XP doesn’t deviate from the XP awarded per level very quickly. It will eventually start deviating but not as quickly as some games. For this reason, high levels wouldn’t be unlikely.
XP is granted based on a weighted score based on time survived, and blocks broken. If a player broke 1000 blocks at a score of 0.3 per block, they’re awarded 300 XP which right away is already 6 levels before time survived, and that’s before the level multiplier and the time survived, thus we have to call GetLevelFromXP 6 times over, and so on.
I could save the XP required for each level to a lookup table and re-call the function as needed (ie the entry is missing), however we then run into the issue of it taking more and more memory as players reach higher and higher levels.
For those reasons, I believe the ideal solution here, if it’s even possible, would be to come up with a single formula that can calculate the level from the XP the player has, but if it isn’t something that is mathematically possible then the cache method would work to avoid recalculating levels from 1 to an arbitrarily large number.
The reason I didn’t design the level system like this is that the XP doesn’t reset per level. It just keeps accumulating over time (like GTA online’s leveling system if you’re familiar with that game).
The reason I do this is because we’re going to have a level leaderboard, and the players should be displayed based on their progress in the level.
So suppose that players at rank 1, 2, and 3 are all level 100
How would I determine in which order to rank them? The best option in my opinion, to not leave it up to luck, would be to display them based on their progress in the level, which is based on their total XP. Total XP would be saved to the OrderedDataStore, then just display the level equivalent to the total XP to the user.
Also what happens in the case of us switching up the formula? The best solution in order to ensure the player isn’t losing any XP/isn’t gaining any XP in the case of a formula change would be to calculate the required XP per-server.
Overall there are quite a few outlying factors that need to be taken into account with this whole system with respect to memory usage, predictability, etc which is why I didn’t go the typical route of level > reset XP to 0 > level, and so on.
This game is still not released to the public so I am fine with changing features around but to me it seemed like this structure of levelling and such was probably the way to go.