countdown timer with seconds and milliseconds

H

hamid1855

Guest
I'm trying to make a countdown timer that counts down from 30 seconds and displays milliseconds also like google's timer. I know how to make seconds but not sure how to do milliseconds. My initial thought was to set off an alarm every millisecond but i dont know what value to set the alarm to. I read somewhere that 30 steps = 1 second (which seems accurate on my computer but i dont know if its consistent on every device) so I assume 0.03 steps is 1 millisecond but you cant set 0.03 for alarms, i think. Any suggestions are appreciated.
 

Tsa05

Member
If your game updates the screen 30 times per second, then you can't count milliseconds. The game would need to update ever thousandth of a second instead of every second.

Instead, every alarm event should just compute how many milliseconds have passed. For example, if your alarm was set to room_speed, then each time the alarm went off, you'd subtract 1000 milliseconds from your timer. If your timer is every 1 step, and room_speed is 30, then every alarm event would subtract 1/30 * 1000 milliseconds.
 

gnysek

Member
Each step is 1/room_speed * 1000 of milisecond. So, if you set alarm to 100, then you can display:

var second = alarm[0] div room_speed;
var milisecond = (alarm[0] % room_speed) / room_speed * 1000;
draw_text(0,0,string(second) + 's ' + string(milisecond));

that will draw in a format "1s 124ms".

if you want a format like "1.45s", just change code var milisecond = (alarm[0] % room_speed); and that's all. If you need to prepend zeroes before, there should be proper script on gmlscripts.com
 
Top