Game config

Hello, how do I access the game config parameters which are used in the new Phaser.Game(config) declaration (in different file)? Namely, I need the parent property of the config.
I tried Phaser.Game.config, but it appears undefined.

Edit: In fact, I need the actual DOM element defined by the parent property.

Hey Coatl,

I think you are misunderstanding the difference between a Class and an Instance.

When you check Phaser.Game, you are checking the constructor for creating a game, not your game.

See here for more info on class vs instance.

Regarding the actual problem:

Make sure you use const game = new Phaser.Game(config). This will make a new instance of your game and call it game.

You can then access the DOM parent with game.domContainer as described in the docs

If you need to access the game object from another file do the following:

When declaring game: export default const game = new Phaser.Game(config)

Then from the top of another file: import game from "./file.js"

If this fails saying that imports can only be at the top level of a module, try adding type=module to your script tag.

Hope this helps!

2 Likes

From a scene, this.game.config.parent is the passed config value (or null), and this.scale.parent is the parent node of the game canvas.

3 Likes

Thank you both, guys!

The game.domContainer is probably going to work, but I went with the samme’s solution (this.scale.parent) which is simpler to use.
You are right about the Phaser.Game status. I wasn’t thinking much about it, just trying and hoping it could be a static method or so… :slight_smile:

1 Like

Strange thing is that this.scale.parent has many undefined values: width, displayWidth, and innerWidth. At last, clientWidth worked for the width detection.