• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Creating a kind of counter/timer?

Traversal

Member
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...
 

flerpyderp

Member
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:

Paskaler

Member
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:

Traversal

Member
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 ?
 

TheouAegis

Member
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.
 
Top