A simple way to display next level amount to reach in the UI

Hello everyone.
If you want to display in your UI field level the amount of the next level, only mimic the health “GetMaxHealthPoints”.
So copy it, past it in your Experience script (after GetPoints Methode for exemple) and rename it like “GetMaxExperience” and change the stat.health by ExperienceToLevelup:

public float GetMaxExperience()

        {

            return GetComponent<BaseStats>().GetStat(Stat.ExperienceToLevelUp);//PV xp du niveau actuel

        }

Then in your experienceDisplay script only tweak the Update by adding the /{1:0} and the second argument , experience.GetMaxExperience():

private void Update()

        {

            GetComponent<TextMeshProUGUI>().text = String.Format("{0,0}/{1:0}",experience.GetPoints(),experience.GetMaxExperience());

        }

Save the two scripts and now you will have displaying the amount of the next level to reach in your UI:

image

Don’t forget to have a xp field for the diplaying enought large to be able to writte the whole numbers in the same line.
Have a nice day.
François

Nice

  1. Try not to GetComponent in Update(). You can cache the text field in Start, and use the cached value
private TextMeshProUGUI experienceField;
private void Start()
{
    experienceField = GetComponent<TextMeshProUGUI>();
}
  1. Your string formatting is a little strange here. Apart from no-one using String.Format() anymore, the {0,0} is left-aligning the text to 0 which does nothing because it’s already left-aligned, and the {1:0} is formatting the text to have no decimal points which an integer don’t have anyway decimals.
    You could just have done this (including point 1)
experienceField.text = $"{experience.GetPoints()}/{experience.GetMaxExperience()}";

I would personally have put an event on experience that fires when it changes and only update the field when that happens, instead of every frame. But that’s just me. Can’t remember what happens in the course

Hello Bixarrio,
really please to read you again as Brian.
My nickname was Soifran in the turnbasegame from Code Monkey in the same forum :wink:
Thanks for the tips.
I’ll think to your event proposition, it sounds logical in fact rather than check it eveyr frame.
I’m a little bit a sorcerer’s apprentice and try many thing by iteration lol :slight_smile: So thanks for your Help.
Have a nice day.
Take Care.
François

Indeed, sooner or later one would want to convert the whole UI handling to be event driven and only making changes when the values changed.
(Which doesn’t rule out one might want to have some nice little lerp for a health-bar or similar UI element that will show changes spread over several frames instead of a sudden jump. Or one might have some nice counting animation for numerically displayed values…)

Privacy & Terms