Sending data from Child scene to the Parent/Sibling scene

Is there a way to send data over from a Child scene to a Parent or Sibling scene? I have three scenes in 3 different .ts files like this:

GameScene.ts (Parent)
----->SceneA.ts (Child)
----->SceneB.ts (Child)

The GameScene starts both the scenes. I know that you can send data from the parent to the child doing something like this:
this.scene.start(‘sceneA’, {test: “hello”})

… but is it possible to send data the other way (from sceneA to GameScene)? I also thought of using something like…
this.scene.get(‘sceneA’).myFunction
…to maybe call functions to grab data, but I always get a warning in my editor (VSCode) that says “Property ‘myFunction’ does not exist on type 'Scene’” and the only way to fix it is to declare a property/method in the phaser.d.ts. This seems fine to me, but is this the “proper” way to do it?

I just started Phaser a few days ago so it’s possible I’m designing my game wrong. I thought it would be a good idea to split my scenes into separate scripts.

EDIT: Post #6 by @Milton also shows an alternate solution.

// level scene
this.events.emit('upPoints' /*…*/);

// HUD scene
this.scene.get('Level_One').events.on('upPoints' /*…*/);

Thanks for the article. I got my scenes to send and receive information properly, but I’m unable to load my images when using the emitter. My images appear as black squares with a green slash. Even when using emit in only one of the 2 scenes causes both scenes to lose their images. Using emit only in the scene that receives information causes that scene to not start at all. Getting rid of anything that uses EventEmitter functions returns my scene to normal.

I’m not really sure what’s causing this to happen. Even though the images aren’t working, I am still able to play my game. There are no errors given in the console.

That’s interesting. It would seem you’re interfering with Phaser’s loading events.
Can you put it online somewhere?

Here’s a pastebin of the relevant code. Every scene has it’s own file:
https://pastebin.com/sCHWgnEW
Basically as a test I’m just sending a number from “GridScene” to “EnemyScene”. It works, but my images do not load when any emitter function is in any scene for some reason.

EDIT: Also there’s a typo in the EventsCenter. It should say export default eventsCenter

I should note that I followed this tutorial when setting up my environment, so I’m using parcel and http-server. I wonder if it’s a problem with that?:
Tutorial

I blame parcel :smile:
No, I haven’t got a clue. Maybe @samme can help.

I see you can also use a built-in scene emitter:

// level scene
this.events.emit('upPoints' /*…*/);

// HUD scene
this.scene.get('Level_One').events.on('upPoints' /*…*/);
1 Like

Wow actually that solved my problem haha. Everything works fine now. My images load fine and the data is able to be sent and received.

I still wonder why the first one didn’t work. I suspect parcel but I’m also not experienced enough to really say anything. Could also just be the way I set everything up.

Thanks so much I appreciate the help.

You can do something like

(this.scene.get(‘sceneA’) as SceneA).myFunction();

Usually those are bad asset URLs. If there are no errors in the console check the Network pane and see what’s getting loaded.