• 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!

Converting seconds to minutes and seconds format

Z

Ziphold

Guest
Hello. This is a very straight forward question.
I have a timer which displays the time in seconds until the box will unlock. However, its kind of silly to see there is "1809" seconds left.
My question is, is there a function or a script that will format the number of seconds to the minutes:seconds format?
example: 1809 would be 30:09

In advance, thank you.
 

Stubbjax

Member
How do you convert seconds to minutes? Answer: You divide by 60.

So 1809 seconds would convert to 30.15. Since we don't want the fractional part, we can floor the resulting value to just get 30. But we also want the remaining seconds for that 30th minute to be displayed as well, which we can achieve by simply modding the entire value by 60, and then adding that onto the string.

Hope that helps!
 
Z

Ziphold

Guest
Yeah, stupid me, its actually really easy.
minutes = totalTime/60;
seconds = totalTime % 60;
 

Yal

🐧 *penguin noises*
GMC Elder
I'd use div and mod instead... / will give you a non-integer amount since all numbers in GML are doubles, and I'm pretty sure % isn't implemented as an operator (unless they've added it in some minor update), so you need to type out 'mod' instead.
 

FrostyCat

Redemption Seeker
I'd use div and mod instead... / will give you a non-integer amount since all numbers in GML are doubles, and I'm pretty sure % isn't implemented as an operator (unless they've added it in some minor update), so you need to type out 'mod' instead.
% has been added as an alias for mod on GMS for quite some time now, just try it on any 1.4 version and see for yourself.
 

Yal

🐧 *penguin noises*
GMC Elder
% has been added as an alias for mod on GMS for quite some time now, just try it on any 1.4 version and see for yourself.
In that case, I'd suggest a slight rewrite of the manual to add an example using % as well. The current presentation seems to suggest % is an just explanation of 'mod' rather than something you can use in code. (Both % and mod in the manual link to this page).
upload_2016-8-29_19-56-17.png

Actually, when reading through the page, there is a similar issue with and, or, xor as well, where the comments in the example gives the impression the symbols are the only option. (Not to mention the inconsistent spacing making my obsessive side uneasy...)
upload_2016-8-29_20-1-22.png

Not really big issues compared to, say, the old description of the instance_id array, but there's still a little bit of ambiguity.
 
Top