[SOLVED] Timer Implementation

Hi all,

I am looking to implement a countdown timer in my game. How do I do this?

1 Like

Thanks @wturber, but how do I start the timer when I need it to?

If i use the example provided from Will’s Code in the topic above, add a Boolean variable to toggle your timer by calling a function when you need to start your timer.

var Timer = pc.createScript('timer');

// initialize code called once per entity
Timer.prototype.initialize = function() {
    this.timer = 0;
    this.isTimerActive = false;
};

// Begin timer by calling this function
Timer.prototype.beginTimer = function(){
    this.isTimerActive = true;
};

// update code called every frame
Timer.prototype.update = function(dt) {
   if(!this.isTimerActive)
        return;

    this.timer += dt;

    if (this.timer > 10) {
        // 10 seconds has elapsed - do whatever you need here...

        // Reset the timer
        this.timer = 0;
        this.isTimerActive = false;
    }    
};

Thanks @T_Froggo! This was very helpful.