Rui Silva
Rui Silva

Reputation: 21

How to Get Data from other Scene in Phaser3?

I've seen answers to other questions similar to mine, with passing the data through scene.start() adn getting it with the init function. But I am asking something different.

Let's say I am on Scene A, which is starts Scene B and is paused. Then I do some calculations on Scene B and I want to pass that result to scene A, when I resume it.

Is there any way to do this?

Upvotes: 0

Views: 1454

Answers (1)

Harry Scheuerle
Harry Scheuerle

Reputation: 456

https://stackblitz.com/edit/so-phaser-resume-data

Similar to passing data with start. There’s also an optional data parameter for calls like resume. Which is passed to the listener of scene resume events.

// scene-a #create
this.events.on('resume', (scene, data) => {
    this.textObj.setText(data.someMath.toString());
});

// scene-b
this.game.scene.resume('scene-a', {someMath});

Alternatively, you can scope something from another scene and register a resume event listener from there instead.

// scene-b
const sceneA = this.game.scene.getScene('scene-a');
sceneA.events.once('resume', () => {
    sceneA.textObj.setText('something from this scene, scene-b');
});
this.game.scene.resume('scene-a');

Upvotes: 6

Related Questions