phaser icon indicating copy to clipboard operation
phaser copied to clipboard

game.scene.scenes.length is set to 0 / expected : 1

Open evan-boissonnot opened this issue 1 year ago • 4 comments

Version

  • Phaser Version: Phaser v3.55.2-FB (Headless | HTML5 Audio)

  • Operating system: Windows 10

  • Browser: Chrome Version 103.0.5060.114 (Build officiel) (64 bits)

Description

I want to jest my phaser game (scenes and prefabs)

First I want to validate Phaser game and first scene are ok to continue my unit tests.

I'm creating a Phaser game, with HEADLESS type (for unit test purpose).

When I want to get my scene from phaser game, I'm not able to do it.

Example Test Code

const game = new Phaser.Game({ type: Phaser.HEADLESS, scene: { preload: () => { } } });

console.log('game.scene.scenes', game.scene.scenes.length); => return 0 => Why ?

So this : const scene = game.scene.getAt(0); console.log('loadGame::scene', scene); it displays null

To add infos : I'm using jest inside a react project (created with create react app CLI)

Here the code I'm using to call game creation

describe('PlayerPrefab', () => { beforeAll(() => {});

beforeEach(() => {
    game = loadGameWithTestScene();
});

Additional Information

In my game (in chrome browser), without unit test, i haven't get any error.

evan-boissonnot avatar Jul 05 '22 22:07 evan-boissonnot

I think that's normal. Use the postBoot callback instead.

samme avatar Jul 24 '22 17:07 samme

@samme you mean : https://newdocs.phaser.io/docs/3.55.2/focus/Phaser.Core.Config-postBoot ?

evan-boissonnot avatar Jul 25 '22 10:07 evan-boissonnot

Yes.

samme avatar Jul 25 '22 16:07 samme

The Scene passed in the game config is added to the internal and private _pending array until the Scene Manager receives the READY event from the Game instance. After this, it will process the pending array and create Scenes from the given data. Only once this is complete (POST_BOOT) will the Scene array have anything in it.

photonstorm avatar Jul 25 '22 17:07 photonstorm