Display timer and score

Are there any tutorials about how you can display let’s say a timer counting seconds and how to update in realtime a HUD when you for example collect items and you want the character’s total collected items to be incremented by one? Would be useful, as I don’t really know where to start implement this.

Well, maybe we’ll start from the simpler thing: HUD.

First, you have to make Scene Variable called for example items collected (that will be how I’ll refer to it for the rest of the tut).
After making this variable, you’ll need a Text Object (there are other ways of drawing text in GD, but this is simplest one). We’ll call it Score. Then set it text to “Score: 0”. We could make separate label for score, but that would be hassle. Then place our Score object in scene

Now, let’s make actual implementation:
First, in event when you collect item (which is probably now collide with item->delete object item), add action “Do +1 to scene variable items collected” (in example posted bellow adding scores happens on pressing up arrow to not blur code with movement/collecting item code).
Then you’ll need to add event without condition (i.e happens constantly) with only one action: Do ="Score: "+VariableString(items collected) to the text of Score.

That’s all - you have your score counter!

Next thing: Timer.

This is a bit more complicated, but only a bit. You’ll need two scene variables Seconds and temptimer. Also you’ll need Text Object to display seconds, which we will call Timer (creative, huh?). Set text of Timer object to “0” (zero).

Actual implementation:
Make another event with no condition. You can wrap it up with event that updates Score object, thought I’ll use separate for clarity. In this event add action Do +TimeDelta() to variable temptimer.
Now add another event, this time with condition Variable temptimer is >=1.
The reason we don’t use =1 is that temptimer can go from less than 1 to more than 1 in single step depending on TimeDelta() so condition may not always work. Now to this event add following actions:

  • Do +1 to variable Second
  • Do =0 to variable temptimer.
    Now last thing - timer update event. Event w/out condition, action Do VariableString(Seconds) to the text of Timer.

Game.gdg (9.34 KB)

//edit: Also, since you adapted platformer example as well, could you help me with issues I have with it in exchange for help?

1 Like

Is there any easy way displaying minutes as well as seconds?

You can add a variable named “Minutes” and a condition like this :

if Variable(Seconds) >= 60 then
Do =0 to Seconds
Do +1 to Minutes

And, finally, an action to print in the object “Timer”
ToString(Variable(Minutes)) + " : " + ToString(Variable(Seconds))

No, do not do that! :slight_smile: Uses only one variable for a timer.
Instead, uses one variable which will act as a timer, and uses this kind of formula to display it :


Just replace TimeFromStart by Variable(TheNameOfYourVariable).

By the way, to have a precise timer using a variable, uses an event like this:

Conditions : None
Actions : Do +TimeDelta() to the variable MyTimer

( TimeDelta() returns the amount of time passed on the last image, see the last paragraph of this page : wiki.compilgames.net/doku.php/en … s_concepts ). In fact, Game Develop timers are internally created using a such process ( Adding TimeDelta() to a variable each frame ).

Finally, since the latest version, there is an expression to get the time elpased in a timer :smiley: ( I do not remember its name, browse the list in the expression editor ). So that you can use a timer and the formula written above ( Just replace TimeFromStart by the expression to get the time elpased in the timer ).

Thanks for your answers, folks.

I’ve tried following your suggestion 4ian, but I guess I’m doing something wrong here. From your explanation I thought I was supposed to write “Variable(“myvariable”)” instead of “TimeFromStart()”, but doing this gives me a compilation error.


My bad, the modulo operator ( % ) must is now a function ( mod ).
Here is the correct formula :


Ah, thanks. I’ll try this as soon as possible. Seems kind of complicated, I have to admit :slight_smile: How are we “normal” people supposed to know how to write this on our own? :stuck_out_tongue:

It is easyer than it looks : handling with some “floors” and variables :slight_smile:
But don’t worry,you will learn
Don’t be stopped by a formula :wink:

where do I find

I’ve followed so far, I just cant find this