GML Visual How to show countdown timer?

M

Mtzgamer01

Guest
I have a timer set for 9000 ticks, or five minutes, but I would like to be able to show that to the player in the format minutes:seconds. Is there a way to do that through drag and drop functions? Or do I have to use code?
 

TheouAegis

Member
One tick is 1/room_speed seconds. So your timer is 9000/room_speed seconds long. You should set the timer to 300*room_speed though, not 9000 explicitly.

Anyway...

var disp=timer div room_speed,
var secs = string(disp mod 60),
var mins = string(disp div 60);
draw_text(x,y,mins+":"+secs);
 
Last edited:
M

Mtzgamer01

Guest
Ok, I entered that code under a sprite in the particular room, and it gave me a bunch of errors. I refined it down to:
Code:
var disp is_real(alarm[0] div room_speed)
var secs is_string(disp mod 60)
var mins is_string(disp div 60);
draw_text(x,y,mins+":"+secs);
And it gave me the error that "disp" is not a variable, and I could not refine it any farther. What am I doing wrong?
Btw this is on a Mac if that makes a difference.

Thanks
 

sp202

Member
That's because you haven't assigned anything to any of the variables; you need to use '='. There's no need for is_real and is_string either, in fact, they're a hindrance.
 
Last edited:
M

Mtzgamer01

Guest
I used '=' but it told me it was another error. That is when is typed 'is' and it gave me those suggestions.
 
J

JFitch

Guest
Is there a reason to declare all those variables? I'd just use this.

Code:
draw_text(x,y,string((alarm[0] div room_speed) div 60)+":"+string((alarm[0] div room_speed) mod 60));
 

TheouAegis

Member
I forgot about string formatting.

Code:
if timer
    timer -= 1;
var disp = timer div room_speed;
var secs = string_replace(string_format(disp mod 60,2,0)," ","0");
var mins = string_replace(string_format(disp div 60,2,0)," ","0");
draw_text(8,0,mins+":"+secs);
 
M

Mtzgamer01

Guest
Ok, I did exactly what @TheouAegis said, and it gave me this error message before the game even loaded:
Code:
FATAL ERROR in
action number 2
of Create Event
for object obj_Avatar:

COMPILATION ERROR in code action.
Error in code at line 3:
       var disp = div room_speed;

at position 10: Unexpected symbol in expression.
Position 10 is the '='.
This is what led me to use is_real and is_string in the first place.
 

TheouAegis

Member
Post the code that you have (not my code), because you messed up. The fact you got the last error showed that you didn't copy everything I posted and odds are you made the same mistake twice.
 
M

Mtzgamer01

Guest
Here is the exact code that I copied from my game and pasted here:

Code:
if timer
   timer -= 1;
var disp = timer div room_speed;
var secs = string_replace(string_format(disp mod 60,2,0)," ","0");
var mins = string_replace(string_format(disp div 60,2,0)," ","0");
draw_text(8,0,mins+":"+secs);
 
M

Mtzgamer01

Guest
The same that I have been getting: that the '=' is causing the error, not any specific code.
 

sp202

Member
Yeah right, that code above seems perfectly fine, no way it's got to do with the equal sign. Post the full error message.
 

FrostyCat

Redemption Seeker
Guys, the error message is clearly in legacy format, having var and = on the same line wasn't allowed until mid-way through GMS 1.2.
Code:
var disp, secs, mins;
disp = timer div room_speed;
secs = string_replace(string_format(disp mod 60,2,0)," ","0");
mins = string_replace(string_format(disp div 60,2,0)," ","0");
 
M

Mtzgamer01

Guest
That fixed the problem with the equals sign. Thanks for that. But, I had to define the var timer as alarm[0] = room_speed * 300 //five minutes. The timer itself works perfectly, but it still doesn't display in game.
Here is my most recent code:
Code:
alarm[0] = room_speed * 300
var timer;
timer = alarm[0]
if timer
   timer -= 1
var disp, secs, mins;
disp = timer div room_speed;
secs = string_replace(string_format(disp mod 60,2,0)," ","0");
mins = string_replace(string_format(disp div 60,2,0)," ","0");
draw_text(8,0,mins+":"+secs);
 
M

Mtzgamer01

Guest
It was under the Create event. So I moved it to a Draw event, and now the timer shows, but sticks at 4:59. And the Avatar object doesn't spawn visibly in the room.
 
M

Mtzgamer01

Guest
And is there a way to possibly make the timer stand out more too? Like a bolder/more colourful font?
 

sp202

Member
Seems like your timer wasn't working at all in the first place if it was all in the create event. Only the initial setting of the alarm should be in the create event. The rest should be in the step event besides draw_text which of course goes in the draw event.

You can bold fonts by using a font other than the default one. Colour can be changed by using draw_set_color before drawing text.
 
M

Mtzgamer01

Guest
I tried splitting up the code into the different events, but it couldn't recognize variables defined in other events. So I tried putting all of it in the Draw event, and the specific parts in their respective events, but I still had the same problem of no Avatar and the timer stuck at 4:59.

The draw_set_color worked for the timer though.

Sorry if I seem uneducated, I am just starting with GM.
 

FrostyCat

Redemption Seeker
If you need the variables recognized in later events, don't declare them with var. It's that simple.

The Create event is for setting up instance variables and perform other kinds of one-time setup. The timer variable is one of them.
Code:
timer = 300*room_speed;
The Step event is for actions that should run once every frame. Decrementing timer is one of them.
Code:
if (timer > 0) {
  timer -= 1;
}
The Draw event is for drawing actions that should run once per active view. Drawing the time is one of them.
Code:
var disp, secs, mins;
disp = timer div room_speed;
secs = string_replace(string_format(disp mod 60, 2, 0), " ", "0");
mins = string_replace(string_format(disp div 60, 2, 0), " ", "0");
draw_text(8, 0, mins + ":" + secs);
disp, secs and mins aren't needed outside this event, so they should be declared with var in local scope. timer is needed in other events, it should stay var-free and be declared in instance scope.

I suggest that you read the Manual's entries on variable scope and events, simply because you are in no position to benefit from tutorials or written instructions without these basic facts. And if you are just starting out, use a more recent version of GM.
 
M

Mtzgamer01

Guest
OK, so I read the entries and they were quite helpful. Thank you for that.

Then I entered the code under their respective events, and the timer works, but goes twice as fast as it is supposed to i.e. two of it's seconds = one actual second. Then, I have the code under the main character sprite, and the sprite does not spawn visibly in the game with this code in action. It did spawn before I entered any code.

Also, the reason I am using an older GM is because I am running it on a Mac and had downloaded it about a year ago, I just never got around to messing with it.

Thanks a lot though for everyone's help so far.
 
M

Mtzgamer01

Guest
I set room speed to 60, but it didn't change the timer speed. I entered room_speed = 60 in the creation code of the room, and set it in the slot that is labeled room speed in the room settings. Thanks
 
Top