How access game instance properties from scenes?

I must be missing something in the docs, but it’s not clear to me how to create globally available properties for a Phaser game. I’m working in Typescript.

My goal is to have a helper class that I instantiate only once, and then is available in all scenes.

I came up with the following, perhaps someone can shed some light on this? Is this the correct way to go about it?

export class SuperCoolGame extends Phaser.Game {
    public arcade:Arcade
    constructor(config: GameConfig) {
        super(config)
        this.arcade = new Arcade()
    }
  }
}

And then from a Scene:

export class GameOver extends Phaser.Scene {

    constructor() {
        super({key: "GameOver"})
    }
    
    create(): void {
        let supercoolgame = this.game as SuperCoolGame
        supercoolgame.arcade.someFunction()
   }
}

Your current solution is perfectly fine as long as you make sure you don’t accidentally overwrite an existing property on the Game instance.

You can also use the registry property of the Game. It is a DataManager, which is a Phaser class used to store and access arbitrary data. If you need to access your helper class often, going through a DataManager is likely to just be hindrance, so I’d leave the code the way it is.

1 Like

Thanks! Just wanted to make sure I’m not missing a Phaser option. The Phaser docs are somewhat impenetrable…

I know about the registry, but I thought that was meant for values like strings and numbers.