GMS 2 Creating a kind of counter/timer?

Discussion in 'Programming' started by Traversal, May 31, 2018.

  1. Traversal

    Traversal Member

    Joined:
    May 1, 2017
    Posts:
    64
    Hi, I really would like to understand how I could create some simple counter.
    What I want to do is show the user a countdown from e.g. 5 minutes.
    You might know such a thing from CoC or other games. It shows how long until
    a unite is produces, gems earned or such kind of things.

    How could I start with this? maybe setup an external database
    and write the time into a db-field? But I would have to check and query the database again and again
    to display the remaining time?

    Or am I thinking too complicated??

    The remaining time should be displayed above an object, just as a healthbar would.
    But counting down the seconds until 0...
     
  2. flerpyderp

    flerpyderp Member

    Joined:
    Apr 26, 2018
    Posts:
    205
    A simple timer example:
    Create:
    Code:
    timer = room_speed * 20; //Initializes the timer as 20 seconds
    
    Step:
    Code:
    if (timer > 0)
    {
       timer --;
    }
    else //Perform action
    
    If you want to display the number of seconds remaining above an object, you could simply use:
    Draw:
    Code:
    var oY = -20; //Use this variable to set the offset above the sprite's origin
    draw_text(x,y+oY,ceil(string(timer/room_speed)));
    
     
    Last edited: May 31, 2018
  3. Paskaler

    Paskaler Member

    Joined:
    Jul 4, 2016
    Posts:
    315
    Reusable timers expressed through 3 scripts:

    Code:
    ///timer_create(duration)
    
    var timer;
    
    timer[0] = 0; // current time
    timer[1] = argument0 * room_speed; // multipled by room speed so we can pass the duration in seconds as opposed to steps
    return timer;
    // store this timer in an instance variable for future reference
    
    // since you are on GMS2, you can just do
    return [0, argument0];
    // instead of all the code above
    
    Code:
    ///timer_tick(timer)
    
    // pass the timer created from above script
    return (argument0[@0]++) >= argument0[1];
    
    Code:
    ///timer_reset(timer, [new_duration])
    
    var t = argument[0];
    
    if (argument_count == 0)
       t[@1] = argument[1];
    
    t[@0] = 0;
    
    You can then use this like so:
    Code:
    /// Some object's create event
    
    my_timer = timer_create(1); // 1 sexond duration
    
    Code:
    // This snippet should be in some code that executes every tick(eg. Step event)
    if (timer_tick(my_timer)) {
    // timer is complete, do whatever, maybe reset it
    }
     
    Last edited: May 31, 2018
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,739
    Also, alarms does this; each object has like a dozen counters built-in.
     
    Bentley and Desix like this.
  5. Traversal

    Traversal Member

    Joined:
    May 1, 2017
    Posts:
    64
    Thank you all for this great answers!!!

    I want the timer to keep counting down, even if the application is closed.
    I did read about local notifications, which I would Trigger once it hits Zero.

    But, what is the best way to keep it counting? I can only think of storing it
    on a Website and run a cronjob ?
     
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,906
    Well, wouldn't you just get the current time when the player starts building something, add however long it's going to take to build it to the current time, past that as the push notification time, and save that time to the instance? In-game, display how long it is until the goal time.

    Never mess with this stuff, but that's how I would go about it.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice