HUD - Add 0's to unused number places.

Just wondering-

I have a HUD for my shooter that has the number of pieces of ammo go down every time you shoot.
Problem is, my max ammo is 150, and I want it to go down to 000 when I’m out, but instead it just shows 0.
How would I add 0 to a number to a number place I’m not using in my HUD?

Thanks.

What I’m talking about (the zeros I would want added are bolded.):

150 - 100

099 - 010

009 - 001

My Blueprints:

In my HUD:
http://i.imgur.com/IZf5wwR.png

In my Event Graph in my character:
http://i.imgur.com/WUR4m0h.png

Thanks,
Joel.

Bumperoo!

in the HUD, instead of casting the Int into Text, you should first cast it into a string. as a string, you can use Len to get the number of characters in the string, as an integer.

you can make an integer variable called MinimumDisplayDigits, and set its default value to 3. if the Len of the string is less than MinimumDisplayDigits, you can subtract the Len of the string from MinimumDisplayDigits, and use the result as the LastValue in a ForLoop, which can append placeholder 0s to the string. when the forloop completes, or when the Len of the string is not less than MinimumDisplayDigits, you can cast the string to text, and display it in your HUD.

1 Like

Hey, I’m working on it, but I’m more of a visual- are you able to provide one?

Sorry, a tad bit new to the HUD.

Thanks mate.
(If you want my Skype, just ask.)

1 Like

I will love you forever. The day I met you, champagne fell from the sky. Butterflies filled the air. It was beautiful, because you are. I will eternally hold you deep within my soul. You are the apotheosis of human excellence, progressiveness, and knowledge.

Thank you Omnicypher <3

Another way to do this is to append your number as a string to a set number of zeros and then use the “right” bp function to only take the right part of the string that you want.
Example: I want a character string representation of a number that has a leading zero if only one character. In other words if the value is 0-9 then display as 00-09, but if 10-99 then display 10-99.

250697-capture.jpg

11 Likes

Much simpler solution than accepted answer.

A shame he was about three years late lmao

Here’s my answer, for historical purposes.

Within a C++ blueprint library, use the following function:

FString UMyBlueprintLibrary::LeadingZeroes(int32 number)
{
	char buffer[4];
	snprintf(buffer, sizeof(buffer), "%03d", number);
	return buffer;
}

This will expose a function to your blueprints that adds leading zeroes to a three digit number. If you want more leading zeroes, change the “3” in the “%03d” in the function and recompile the C++.

Hope this helps someone!

1 Like

It’s very simple. Just use the ‘To Text (integer)’ node. Hit the down arrow to expand the node, then type 2, 3, or 4 (or however many numbers you want) in the 'Minimum Integral Digits box. Whallah! You now have preceding Zero’s on your single digits.
So if you type 3 in the box, this will turn 0 into 000 and 32 into 032 etc.

336647-totextnode.png

18 Likes

Now THIS should be the answer.

2 Likes

Very Smart!

1 Like

This worked the best

1 Like

Nice idea. I created a macro based on your answer.

#define FORMAT_NUMERIC_STRING(integer, depth) FString::Printf(TEXT("%0"#depth"i"), integer)

i.e.: if you invoke this macro like here:

int32 MyNumber = 87;
auto MyString = FORMAT_NUMERIC_STRING(MyNumber, 5);

the value of MyString will be equal to “00087”.

1 Like