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
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

Show countdown timer upon key press?

Discussion in 'Programming' started by traparvvaves, Apr 12, 2017.

  1. traparvvaves

    traparvvaves Guest

    Hello, I am relatively new to programming in GameMaker. I am currently working on a part where if you press a key, a countdown timer will appear on the screen. However, I haven't had much luck in having it update. When I press the A key, it does show that there is 5 seconds on the clock, but it doesn't draw the countdown. This is the code I have for it:

    CREATE EVENT:
    Code:
    global.battery = 1;
    countdown_time = 5.0;
    rounded_time = 0;
    
    STEP EVENT:
    Code:
    if keyboard_check_released(ord('A')) {
        if global.battery > 0 {
            if (countdown_time > 0) {
                time = delta_time/1000000;
                countdown_time -= time;
                rounded_time = round(countdown_time);
            }
            else {
                countdown_time = 0;
                show_message(countdown_time);
                }
            }
        else {
            show_message("No batteries collected.");
            }
    }
    
    DRAW GUI EVENT:
    Code:
    draw_text(32,32, rounded_time);
    
    Can anyone help me with this? Thank you!
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,449
    you are only updating the rounded time upon key release. So it's going to keep showing the same number until you press the key again.
     
  3. traparvvaves

    traparvvaves Guest

    Thank you for letting me know. I tried to get rid of rounded_time and now I found out that the timer will only run if I keep pressing the key down, but will stop when I release the key. I want to be able to activate the timer to countdown all the way once I press the key once. How will I be able to do that?
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,449
    well, the easiest way would be to set a variable to either true or false depending on if the timer needs to count down. When you press the key, set the variable to true. Check if the variable is true and if it is count the timer down. When the timer hits zero, set the variable to false.
     
  5. Kuro

    Kuro Guest

    To your create event add
    start_clock = false;

    Then change step to something like this:
    Code:
    if keyboard_check_released(ord("A"))
    {
       start_clock = true;
    }
    
    if (start_clock)
    {
    
        if global.battery > 0
        {
            if (countdown_time > 0)
            {
                time = delta_time/1000000;
                countdown_time -= time;
                rounded_time = round(countdown_time);
            }
            else
            {
                countdown_time = 0;
                show_message(countdown_time);
            }
          }
          else
          {
                show_message("No batteries collected.");
           }
            
    }
    
     
  6. traparvvaves

    traparvvaves Guest


    Thank you so much, it worked! I had been wracking my mind for the past few hours on this, I didn't realize it was so simple! Thank you both!
     

Share This Page