• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

how to start a number at 00000 instead of 0

F

fxokz

Guest
There are some games (retro styled) that make the score look like 00000 and as the player gets more points or whatever it starts to look like 00300 or 45000. How do they achieve this? Same with times.. instead of 0:0 what about 00:00?
 
T

The5thElement

Guest
I believe the best way to achieve what you are looking for is to convert the score and time to a string.

For example:
Code:
//Draw Event
var scoreString = string(score);

if(score <= 9){
     draw_text(x, y, "0000" + scoreString);
} else if(score >= 10 && score <= 99){
     draw_text(x, y, "000" + scoreString);
} else if(score >= 100 && score <= 999){
     draw_text(x, y, "00" + scoreString);
} else if(score >= 1000 && score <=9999){
     draw_text(x, y, "0" + scoreString);
} else if(score >= 10000){
     draw_text(x, y, scoreString);
}
 
Z

zircher

Guest
Heh, while nice and short, the larger code block actually does the job unless you add a second function to replace the leading spaces with zeroes.

str2 = string_replace(str1, ' ', '0');
 
Another method, for giggles:

Code:
var num_digits = 5, // Can also be a macro value
    score_text = string(score);

score_text = string_repeat("0", num_digits - string_length(score_text)) + score_text;
 

TheouAegis

Member
A lot of old games had one variable for each digit in the score. But it's a lot easier to work with the strings in GM.
 

Alexx

Member
This is what I used in a recent project, to pad out to 6 characters:

Code:
score_text=string(score);
while string_length(score_text)<6 score_text="0"+score_text;
 

hippyman

Member
Lol I feel stupid for not thinking about that function. My example is big and bulky compared to it.
Don't sweat it. I did just find out something about the function that I didn't realize at first. It doesn't add zeroes like what you're trying to get. What you can do is use string_format and then use string_replace_all to replace spaces with zeroes.

Code:
var str = string_format(56,10,0);
str = string_replace_all(str," ","0");
show_message(str);
Run this code and the result will be a string with "0000000056"

Hope that helps
 

Sabnock

Member
Lol I feel stupid for not thinking about that function. My example is big and bulky compared to it.
i do the same as you but slightly differently. but at least we can say we programmed a solution for ourselves :D wish i'd known about the format function first though lol.
 
Top