Precise time control in a scene

I am using Phaser 3.23 to develop an experimental task (a.k.a online game) which require a trial to run 10 seconds long.

In the main scene, in the create() function, I added:

 this.time.delayedCall(10000, trialEnd, [], this);
 trialStartTime = this.time.now;

In a separate trialEnd function, I added:

trialEndTime = this.time.now;
let duration = (trialEndTime - trialStartTime)/1000;
console.log(duration);

Apparently, the first trial duration was 45 sec, and 12 sec for 2nd, 3rd, and the last. This is not an extremely time sensitive task, but would still prefer to have consistency across trials. Regardless, none of the trial duration was close to 10 secs which I set up in the time.delayedCall() function.

I am not sure whether this difference is due to not using time.delayedCall() properly, or more with my line of codes (when exactly I am calling to register time.now).

Here is a link to the task, and thank you for any useful feedback.

Best,
QN

That does seem peculiar. The timer event code looks correct.

I changed the scene to autoStart = false, and that gets me closer to a more precise timing. However, the first one is always kinda off compare to the rest.

Best,
QN

What i do is that i have a variable with the start time (date.now), and then in update i check if date.now-startTime is above the time limit, and if it is then i stop the current game

2 Likes

I am following up with this issue. After collecting some pilot data, I made a histogram of the trial duration of 216 trials. As you can see, the problem remained, that the duration for the first trial tends to cluster around 200 seconds (since game start)?

Here is a closer look at the lower range:

Although not all trials perfectly landed on a 30 seconds duration. Most trials are around that range.

I am not sure how to solve this problem. The subjective experience of user time doesn’t seem to feel different, but the computation outcomes varies. Here is the link to the task.

Any suggestions or advice will be appreciated.

Thank you so much for your time.

QN

In earlier Phaser v3 I think this.time.now is not properly set during scene create() and would be 0. The timer event duration would still be correct though. Only trialStartTime would be wrong.

so it’s better for me to access this.time.now at a different code line? or is it better for me to update Phaser to the latest version?

No, the this.time.now problem should have been fixed in Phaser v3.17. Yours must have something else happening.

Have you actually witnessed one of these overlong durations?

Is there any way the game could be getting paused/closed in between?

You are right that the timer event is working properly (I timed it myself for the first trial), and I think the duration problem is due to people switching window during the game which can lead to pausing the game.

Thank you for the support!