Jump to content

game.time.reset doesn't work


ForgeableSum
 Share

Recommended Posts

I'm having some difficulty with game.time.reset(). It doesn't do what I think it's supposed to do and that is, set the game clock back to zero.

 

If you check game.time.now before and after the reset, the time is if it hadn't been set back to zero. 

 

If that's not what it's supposed to do, how do I set the clock back to zero as if the page were first being loaded?

 

Should be using a separate timer for my own purposes as I'm sure game.time is used by Phaser Core? All I want really, is a timer which resets after I so choose (e.g. when I reset the current game state). Is game.time not the right thing to use for that?

 

Maybe I should just use native js to do this? 

Link to comment
Share on other sites

In the file Time.js you can see the reset function (line 507) and all it does is set an internal private variable _started to be equal to the current time.

When you use the time functions to know how much time has elapsed, it will subtract the value in _started from that value.

I imagine it does it this way because it may not be possible to reset the actual system time value to zero on all devices.

Looking a bit further, this.time is set in the boot function to Date.now() so indeed, it is not possible to set it to zero because the Date is not zero.

You can use game.time.totalElapsedSeconds() to get a Number that represents how many seconds the game has been running.  After you call reset that will go back to zero because it uses _started.  The value is floating point so although it's in seconds you can check for fractions of a second too.

Link to comment
Share on other sites

In the file Time.js you can see the reset function (line 507) and all it does is set an internal private variable _started to be equal to the current time.

When you use the time functions to know how much time has elapsed, it will subtract the value in _started from that value.

I imagine it does it this way because it may not be possible to reset the actual system time value to zero on all devices.

Looking a bit further, this.time is set in the boot function to Date.now() so indeed, it is not possible to set it to zero because the Date is not zero.

You can use game.time.totalElapsedSeconds() to get a Number that represents how many seconds the game has been running.  After you call reset that will go back to zero because it uses _started.  The value is floating point so although it's in seconds you can check for fractions of a second too.

Thanks but I'm afraid game.time.totalElapsedSeconds() doesn't go back to zero when you reset the game. 

Link to comment
Share on other sites

It surely must... unless the code has been changed.  The relevant code from Time.js is:

    /**    * The number of seconds that have elapsed since the game was started.    *    * @method Phaser.Time#totalElapsedSeconds    * @return {number} The number of seconds that have elapsed since the game was started.    */    totalElapsedSeconds: function() {        return (this.time - this._started) * 0.001;    },    /**    * Resets the private _started value to now and removes all currently running Timers.    *    * @method Phaser.Time#reset    */    reset: function () {        this._started = this.time;        this.removeAll();    }

So reset sets this._started equal to this.time, and totalElapsedSeconds() subtracts this._started from this._time.  The result has to be zero unless something else has changed the value of this._started or this.time in between your calls.

Link to comment
Share on other sites

It surely must... unless the code has been changed.  The relevant code from Time.js is:

    /**    * The number of seconds that have elapsed since the game was started.    *    * @method Phaser.Time#totalElapsedSeconds    * @return {number} The number of seconds that have elapsed since the game was started.    */    totalElapsedSeconds: function() {        return (this.time - this._started) * 0.001;    },    /**    * Resets the private _started value to now and removes all currently running Timers.    *    * @method Phaser.Time#reset    */    reset: function () {        this._started = this.time;        this.removeAll();    }

So reset sets this._started equal to this.time, and totalElapsedSeconds() subtracts this._started from this._time.  The result has to be zero unless something else has changed the value of this._started or this.time in between your calls.

hmmm I wonder if there is something funcky going on with my game time. 

 

I tried creating a new Timer:

gameTime = new Phaser.Timer(game);
 
 
and starting it:
gameTime.start(); 
 
But the timer stops at an arbitrary number (e.g. 25 or 260). I know this from reading it with gameTime.ms or gameTime.seconds. 
 
Funny thing is, when I check gameTime.running, it returns true ... so it's running but the ms/seconds isn't updating. Damn if I wasn't confused before, I am now. 
 
Link to comment
Share on other sites

Ok, in this new example you are creating a timer (Timer.js file) which works differently to querying the system time (Time.js accessed by game.time).

The Timer.js constructor takes two parameters: game, autoDestroy

and if autoDestroy is undefined it is set to 'true'.  The comment says: "If true, the timer will automatically destroy itself after all the events have been dispatched"

So I'm guessing your timer is stopping because either you didn't add any events, or all of the events were completed.

 

The Timer.js header says:

* A Timer is a way to create small re-usable (or disposable) objects that wait for a specific moment in time,

* and then run the specified callbacks.
 
Sorry this doesn't help you find the original problem but might help resolve some of the confusion :)
Link to comment
Share on other sites

 

Ok, in this new example you are creating a timer (Timer.js file) which works differently to querying the system time (Time.js accessed by game.time).

The Timer.js constructor takes two parameters: game, autoDestroy

and if autoDestroy is undefined it is set to 'true'.  The comment says: "If true, the timer will automatically destroy itself after all the events have been dispatched"

So I'm guessing your timer is stopping because either you didn't add any events, or all of the events were completed.

 

The Timer.js header says:

* A Timer is a way to create small re-usable (or disposable) objects that wait for a specific moment in time,

* and then run the specified callbacks.
 
Sorry this doesn't help you find the original problem but might help resolve some of the confusion :)

 

I missed that about autoDestroy - I guess I just assumed default was false. 

 

But I tried setting it to false and still, the timer is stuck at some arbitrary number. Christ, maybe I should just use native javascript? All I want to do is see the amount of elapsed time from the beginning of the state. So if I reset the state, the timer should go back to zero. 

Link to comment
Share on other sites

Have you checked to see if resetting the state actually calls Time.reset too?  It may not (I can think of justifications for either choice) and that could be why totalElapsedSeconds didn't reset.

Easy fix if that's the case, call game.time.reset() when you reset the state...

I don't think you should need a timer for this, the global Time system should give you all you need.

 

If you use native JS you'll end up having to deal with all the nasty corner cases that Phaser wraps for you... Like navigating away from the page which stops the RAF timer but let's Date.now() continue to increment.

Link to comment
Share on other sites

Have you checked to see if resetting the state actually calls Time.reset too?  It may not (I can think of justifications for either choice) and that could be why totalElapsedSeconds didn't reset.

Easy fix if that's the case, call game.time.reset() when you reset the state...

I don't think you should need a timer for this, the global Time system should give you all you need.

 

If you use native JS you'll end up having to deal with all the nasty corner cases that Phaser wraps for you... Like navigating away from the page which stops the RAF timer but let's Date.now() continue to increment.

That was one of the first things I tried: game.time.reset() nope, it doesn't change the time read from game.time.now. 

 

I still don't understand why this doesn't update the .ms property:

    gameTime = new Phaser.Timer(game,false);
 
        gameTime.start(); 
 
 
why is gameTime.ms always some arbitrary number that never moves? am I missing something here?
Link to comment
Share on other sites

Okay so game.time.totalElapsedSeconds() is actually converting from milli to seconds which doesn't make sense if you want milli. I'm surprised there isn't a function for totalElapsedSeconds (perhaps I should make the a feature request), but it's simply:

 

game.time.time - game.time._started;
 
I'm using that to determine milliseconds from the last time game.time.reset() was called. In my case, I am calling game.time.reset() every time I reset the game. This works for me. 
Link to comment
Share on other sites

 

Okay so game.time.totalElapsedSeconds() is actually converting from milli to seconds which doesn't make sense if you want milli. I'm surprised there isn't a function for totalElapsedSeconds (perhaps I should make the a feature request), but it's simply:

 

game.time.time - game.time._started;
 
I'm using that to determine milliseconds from the last time game.time.reset() was called. In my case, I am calling game.time.reset() every time I reset the game. This works for me. 

 

God dammit. This is no good because the time continues when the game is paused ... I'm really not having a fun time with this. 

Link to comment
Share on other sites

Okay so I've decided to not use the timer at all and instead count update loops with this in my update loop:

 

if (!game.paused) {
            gameTime++;
        }

 

 

Question is: how many times per second does the update loop loop? This actually might be better for me than counting milliseconds since game reset, but I'd like to know exactly how many times per second this is happening... also, will this be the same on every device? I'm thinking long term (multiplayer) here. 
Link to comment
Share on other sites

Sorry, I should've written more. My post was in answer to your question: "how many times per second does the update loop loop?" The one I use most is "physicsElapsedMS"; it's what Phaser uses to drive all the physics calculations in the game and keep them running smoothly under variable frame rates. And, yeah, it should hover somewhere around 16ms ideally – that's 60 FPS (1000ms / 60 FPS = 16ms / frame).

 

I don't believe it relies on any single physics system, so you should be good with no other physics. Tweens use it, for instance.

 

What're you trying to do?

Link to comment
Share on other sites

Sorry, I should've written more. My post was in answer to your question: "how many times per second does the update loop loop?" The one I use most is "physicsElapsedMS"; it's what Phaser uses to drive all the physics calculations in the game and keep them running smoothly under variable frame rates. And, yeah, it should hover somewhere around 16ms ideally – that's 60 FPS (1000ms / 60 FPS = 16ms / frame).

 

I don't believe it relies on any single physics system, so you should be good with no other physics. Tweens use it, for instance.

 

What're you trying to do?

Ah, I understand now. 

 

Here's what I am trying to do:

Every event in my game is recorded with a timestamp. When I want to playback the game, as a recording, I have a function which goes through each event and the timestamp associated with it, then fires the event. 

 

It works great but I have yet to find a reliable time-keeping system that will:

A) Reset to 0 when I so choose (i.e. when I reset the game)

B] Not be variable based on machine environment. This would cause networking problems for multiplayer. 

C) Pause when the game pauses. I wouldn't want to be firing events when the entire game world is paused. 

 

It seems counting update loops fulfills all criteria except B.

Link to comment
Share on other sites

¯\_(ツ)_/¯ 

 

You might be stuck getting one machine to tell everyone else how much time has passed, try and measure lag, accept that there will be mistakes and keep going.

 

Is this in a multiplayer RTS setting? I think you're going to be doing a lot of this kind of thing: http://gafferongames.com/networking-for-game-programmers/what-every-programmer-needs-to-know-about-game-networking/

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...