'Ways to limit Phaser3 update rate?

In my Phaser3 game there is a global gameTick variable that is incremented every update. I am using this to spawn in enemies in my game every 100th update.

Here is a simplifed example of what is going on in my scene class:

update () {
    global.gameTick++;

    if (global.gameTick % 100 === 0) {
        this.spawnAlien();
    }
}

This works fine but as soon as a user plays the game on a monitor with a refresh rate >60hz the update timing breaks and causes the aliens to spawn more frequently.

I have checked this.physics.world.fps and it is 60. I can also modify this.physics.world.timescale but then I would have to do a giant switch statement for every refresh rate.

Either I am missing an obvious solution or my global.gameTick method is not an effective way to accomplish this task.

This is what I have in my config so far

let config = {
    type: Phaser.AUTO,
    backgroundColor: "#000",
    scale: {
        parent: "game",
        mode: Phaser.Scale.FIT,
        width: 1900,
        height: 600,
    },
    physics: {
        default: "arcade",
        arcade: {
            debug: true,
            fps: 60 // doesn't fix update frequency
        },
        fps: { // not sure if this is even doing anything
            max: 60,
            min: 20,
            target: 60,
        }
    },
    pixelArt: true,
};


Solution 1:[1]

To limit the update rate, use the following method.

// The time and delta variables are passed to `update()` by Phaser
update(time, delta) {
    this.frameTime += delta

    if (this.frameTime > 16.5) {  
        this.frameTime = 0;
        g.gameTick++;
        // Code that relies on a consistent 60hz update
    }
}

This accumulates the miiliseconds between the last frame and the current frame. It only runs the update() code if there has been 16.5ms of delay.

The example above works for 60fps, but if you want to limit the FPS to a different value use the formula: delay = 1000/fps.

Solution 2:[2]

You can also set the following property in your game's config object:

fps: {
  target: 24,
  forceSetTimeOut: true
},

Source: https://phaser.discourse.group/t/how-to-limit-fps-with-phaser-3/275/14?u=saricden

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 saricden