Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Way of Creating XP Bar Without Requiring Inverse of Function

Discussion in 'Scripting' started by jlars789, Aug 30, 2023.

  1. jlars789

    jlars789

    Joined:
    Mar 3, 2023
    Posts:
    16
    Hello,

    I deliberated for a long while on the Experience curve I want for my game, as in the mathematical function I want to correlate to player level. I settled on this:

    upload_2023-8-30_16-33-11.png

    I am not really wanting to budge on this - anything logarithmic was too slow growing and everything inverse-polynomial was too quick growing, so I settled on a middle-ground. If you do have an idea for a curve that's similar, but simpler, please do let me know.

    However, the time has come to begin implementing an experience bar. Even after thinking about it for awhile, I can't seem to think about any way of creating a bar that scales accurately without having the inverse of a function, as it needs the to take the percent of the difference between current level and next level to create a scale.

    While implementing an inverse is trivial for most functions, this one requires the use of the Lambert-W function, which requires some relatively heavy computation, as well as the fact that I'm not well versed in its use.

    My ideal goal is creating an XP bar that doesn't require the inverse of the function - my next thought went to using the derivative to estimate how much more XP is needed to get to the next level. Otherwise, my extremely non-optimized answer is to pre-calculate the next level's XP by guess-and checking, which seems extremely inefficient.

    I hope I'm not overthinking this - please do let me know if I am.
     
  2. Dedi6

    Dedi6

    Joined:
    Jan 20, 2019
    Posts:
    119
    First of all, love to see someone using math - I suck at it and had no idea what you talked about but you sound smart!

    As for the bar, I think a Lerp can be useful.
    https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html

    You already have the max XP required, and the current XP. So you have everything you need to get the current percent of the level with this.
     
  3. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,977
    Maybe I’m missing something, but don’t you actually want the bar for current level xp to be linear?

    You don’t even have to do any math, just make an array of all the xp values for each level (using whatever math you want, you can write an editor only script that populates the array automatically), then linearly interpolate between each level and you don’t really need to do any math at all at runtime.
     
    Bunny83 likes this.
  4. jlars789

    jlars789

    Joined:
    Mar 3, 2023
    Posts:
    16
    Thanks for the response! I want the bar between each level, say Level 8 and Level 9 to be linear, despite the fact that function itself is not. I could make the bar's percent progress tied to the decimal portion of the output (i.e., the bar is 70% filled when you're Level 8.7), but that's not true linear. It may be linear enough, however...

    The issue I have with the array is two things, but first I have to mention a design philosophy of mine: I hate seeing integer caps in games. As such, my XP value is a ulong, which means it goes up to 18 quintillion, if memory serves. That equates to 460,000 levels in game - none of which I want to explicitly bar from the player. This means I would have to, theoretically, carry an array of size 460,000 * 8 (size of ulong) = 3.68m bytes, or 3.68 megabytes, in memory at all time. I *could* find a way to only carry the next n levels required, and do one recalculation on each level-up.

    The lerp tip is super helpful, though, thank you!
     
  5. jlars789

    jlars789

    Joined:
    Mar 3, 2023
    Posts:
    16
    I am also excited to use math when I'm making something I enjoy! Thanks for the Lerp tip, it should come in handy.