How to turn a variable (here, an integer) into text using sprite

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Beureuuu

Hello,

I’m not sure my question is very clear so let me explain.

I got an open-source tileset (this one) where there’s numbers in sprite form, for scoring and such I assume. But I wonder how you could turn a variable into those sprites?

So far I went for this structure:
Node structure

Give us that (I pre-allocated the “0” sprite on texture rect):
Result

Should I make a folder with all the numbers sprites, parse them all, load them into an array and load the corresponding digits?
Or can you read it directly from the tileset on-the-go ?

Thanks in advance.

:bust_in_silhouette: Reply From: Whom

Your solution should work and I think it’s the easiest solution in case you want to continue using sprites.

But what if you decide to make a leaderboard or what if you need labels that have both letters and numbers? Using Sprites/TextureRects for numbers can quickly become overly complex. If you plan on using numbers of the same style elsewhere in your game, it might be a good idea to make a custom font using those sprites (I recommend this tutorial) or find another font that fits the style (or maybe you already have a font assuming your game has text). But if you want to use those sprites only for the score, maybe it’s not worth the effort.

Thought so as well, a font would be more efficient and less painful to deal with, definitely.
Thanks for the tutorial!

Beureuuu | 2022-10-23 10:25