# How to compute XP thresholds when scaling changes for each level range?

I am developing mobile game and I am stuck on an issue regarding proper player stats scaling based on level.

As a parameters for the computation we have:

• base XP value
• multiple increment values (for example one increment for levels 1-5, another one for 6-10 and so on...)

We are using this formula: current level XP = previous Level XP + (previous level XP ^ increment).

Let's break it down on an example: We want to get the XP threshold to graduate from level 3. The base XP value is 100, increment for levels 1-5 is 0.5.

To get the needed XP, we first need to get the value for level 2:

level2Value = 100 + 100^0.5

Now we have stat value for level 2 and we are able to get level3Value like this:

level3Value = level2Value + level2Value^0.5.

If we had to calculate values for some high levels, this seems like a rather inefficient way to do things, as values for all previous levels must be computed prior, and in addition we are changing the increment value based on level - we cannot simple have one line of formula which gets us the number that we wan; all is dependent on previous values.

Of course we can compute all previous values, cache it on the server and then do the lookup to the data, but we would like to avoid it if possible - still I have a feeling that there must be better way to deal with this problem.

My question is the following:

Can you suggest some other (similar) formulas or ways to compute this? Ideally just one line of code should do the trick (something like $$$$level X XP = X * baseXp * increment|, but more complex which allows usage of different increment values)

You are overcomplicating it. Doing the math once and storing it in a dictionary or list is perfectly fine. The time and memory footprint for the calculation (or read it from a config/ database) is so small, even if you have 500 or more levels, that a single formula is not worth it. There are even some benefits to not use a single formula:

• You can way easier adjust the correct value. A formula that changes every 5 level over 100 will look like a monster. By doing a small change, did you really manage to change the amount for lvl 29 to 30 or was it perhaps for lvl 65?
• With one look you know how much exp is needed and even people with no math background can make changes.
• The actual footprint will be smaller. For a formula you still would call it at least once per level up. A lookup can be reused for every player server side.
• Making changes in the dev process is faster, instead of coming up each time with a new formula that might reflect what you want

If there is no bottleneck (and if the level calculation is the bottleneck in your game you have a different problem - usually by calling it too often), don't try to optimize the code too early. For simple things, a simple solution is faster and less bug prone. By the time you are done with a full custom formula once, you could have used that time for other features of your game.

• Thank you for the answer - probably I will do what you suggest. The reason why I was asking is that we actually need to compute multiple values for each level - in the end I will end up with more than 5000 values which need to be saved. As this will be saved to backend (Playfab) it could be a storage problem... but I checked limits and it should be fine in the end. May I ask what you mean by this sentence - "...if the level calculation is yours, you have a different problem..."? Dec 7, 2023 at 12:35
• @JakubM Usually people are trying to optimaze code too early in the development. While neat code is always a bonus, it can spiral really fast out of control in terms of what you actually get done. Most of the times it is better to focus on getting features of the game done than perfectly polishing what actually is working. 100 or 5000 calculations are not much of a difference (you should worry if your game is slow because of the calc), you are doing them once and are done with it. If you are holding them serverside in memory, you can store it in a single string and split it again on loadup. Dec 7, 2023 at 12:47

This is a piece-wise(1-5,6-10) sum of a geometric series.

if (n< 6)
a =100^(1+1/(2*n))
else
a=251.188643150958 + 100^(1/(3*n)) //assuming level6 = level5 + 100^(1/3)..
`

the 251 is n=5 and the starting point for the 6-10 levels.

That is the closed form formula for the desired calculations.