[SOLVED] Set up a Global Timer

Hi guys, I am sorry if the question is a bit dumb, but I just can not figure it out :confused:

I have two different Scenes:

  • Intro Scene
    -Level 1

If you fail to win level 1, you lose a life. If you are in the Intro Scene, the timer to retrieve life gets triggered:

Conditions:

Global variable Lives < 5 the timer "Getlife" is greater than 30 seconds Trigger once

Actions:

Do Globalvariables(Lives)+1 to Global variable Lives reset timer "Getlife"

and my timer logic is : Do 30 - round(TimerElapsedTime("Getlife")) to global variable GetLifeCountdownValue

And it works fine, BUT only if I’m in the Intro scene.
Does anyone have suggestions on how to make the timer work, even if I’m replaying level 1?

Thanks in advance :slight_smile:

Instead a GD timer, use a global variable “LifeTimer” (global so you can modify it from any scene).
To make a variable works as a timer, always increase it with the value “TimeDelta()”, it’s the time elapsed in seconds since the last frame:

Do + TimeDelta() to global variable "LifeTimer"

Test it:

Global variable "LifeTimer" is > 30

And reset it:

Do = 0 to global variable "LifeTimer"

Now you can modify your “timer” from any scene. :slight_smile:

By the way, instead:

Do = GlobalVariable(Lives) + 1 to the global variable "Lives"

you can just:

Do + 1 to the global variable "Lives"

Thanks for the reply! Gonna try it out now.
Aah yes, it looks much cleaner than my method^^

HI, I got the timer to work!^^ But I have one other question: If I want the timer to continue in the different scene, do I have to add all the conditions and actions of the global timer to every level?

Thanks :slight_smile:

NB.: I can’t help but notice that when I change the tabs in the browser, the Timer pauses. Is this normal for the preview of the game, or will the exported game have the same behaviour?

EDIT: For the ones reading in the future, here’s how I got it to work:

Event 1: Conditions: Global variable Lives <5 Action: Do +TimeDelta() to global variable LifeTimer

Event2: Conditions:Global variable Lifetimer >=30 Trigger once Actions: Do +1 To Lives Do =0 To LifeTimer

Event3: Conditions: Global variable Lives >5 Action: Do =0 to global variable LifeTimer

Yes, you could… but External Events were made for things like this: Put the code related to the global timer in an external event, then link to it in every scene you want it to work, there is a special event “Link” to do it (under the green “+” in the events editor ribbon) :smiley:

Haha, that’s true. I don’t know if it should work this way really :slight_smile:

Thanks for the help, gonna look into it :slight_smile:

Well I gotta admit, that worries me a little^^ Do you think it will be possible to link the timer to a DB?

A database? I really have not worked with HTTP requests, or anything related to the network, but it would be possible (if I’m right, SQL and others have a lot of built-in functions related to time) :slight_smile:

Yeah, I think it would be more reliable to save the state of the player to a database, and trigger the life timer from the base. But that would be for further development.

For now, I will mark it as solved, and if I come up with a good way, I will definitely post a tut :slight_smile:

Thanks for the help :slight_smile:

Hi Lizard- 13,

I am using your +TimeDelta() trick for my global timer and it works like a charm. Now I just need to figure out how to pause the timer and I am a bit stuck on that. Could you offer any advice?

Hello! please do not bump threads that are multiple years old and marked as solved. This method should still work, but this thread was for GD4 which was basically an entirely different engine.

At this point, I’d recommend making a new thread and writing up your scenario, what you’re trying to accomplish, and include a screenshot of your current events in question.

With the above in mind, I’m de-bumping this thread and closing it out. Thank you.