1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Drag And Drop How to show countdown timer?

Discussion in 'Programming' started by Mtzgamer01, Feb 19, 2017.

  1. Mtzgamer01

    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?
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,434
    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: Feb 19, 2017
  3. Mtzgamer01

    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
     
  4. sp202

    sp202 Member

    Joined:
    Sep 26, 2016
    Posts:
    968
    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: Feb 20, 2017
  5. Mtzgamer01

    Mtzgamer01 Guest

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

    JFitch Member

    Joined:
    Sep 28, 2016
    Posts:
    428
    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)); 
     
  7. sp202

    sp202 Member

    Joined:
    Sep 26, 2016
    Posts:
    968
    @JFitch For the sake of readability alone, yes. More so if you need the values elsewhere.
     
  8. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,434
    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);
     
  9. Mtzgamer01

    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.
     
  10. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    1,072
    If you did exactly what he posted, then why is your timer variable missing?
     
  11. JFitch

    JFitch Member

    Joined:
    Sep 28, 2016
    Posts:
    428
    you forgot the "timer"

    Code:
    var disp = timer div room_speed
     
  12. Mtzgamer01

    Mtzgamer01 Guest

    I adjusted the code, and the same error occurs.
     
  13. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,434
    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.
     
  14. Mtzgamer01

    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);   
     
  15. sp202

    sp202 Member

    Joined:
    Sep 26, 2016
    Posts:
    968
    What's the error you get now then?
     
  16. Mtzgamer01

    Mtzgamer01 Guest

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

    sp202 Member

    Joined:
    Sep 26, 2016
    Posts:
    968
    Yeah right, that code above seems perfectly fine, no way it's got to do with the equal sign. Post the full error message.
     
  18. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,851
    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");
    
     
  19. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,434
    I was going to get to that once I got home from work, but I just now got home and you got here before me.
     
  20. sp202

    sp202 Member

    Joined:
    Sep 26, 2016
    Posts:
    968
    Ah, that was some good sleuthing.
     
  21. Mtzgamer01

    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);
     
  22. sp202

    sp202 Member

    Joined:
    Sep 26, 2016
    Posts:
    968
    Is your code in the draw event? draw_text goes in the draw event.
     
  23. Mtzgamer01

    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.
     
  24. Mtzgamer01

    Mtzgamer01 Guest

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

    sp202 Member

    Joined:
    Sep 26, 2016
    Posts:
    968
    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.
     
  26. Mtzgamer01

    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.
     
  27. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,851
    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.
     
  28. Mtzgamer01

    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.
     
  29. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,434
    Set your room speed to 60.

    For some reason 30 fps made it count down faster, then changing it to 60 fixed it.
     
  30. Mtzgamer01

    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
     

Share This Page