Events across scenes

Hi,

I wish to emit 2 events across 2 scenes (but in a specific way).

I’ve got a working version which emits an event to another scene and logs in the console.

However, I would like it to work something like this:

    create() {

    this.scene.get('GameSc').events.on('event1', function() {
    
    triggered = true;

    if(triggered){
        this.scene.get('GameSc').events.once('event2', this.handler);
    }

    });

}

handler() {
    console.log('triggered');
    triggered=false;
}

It unfortunately doesn’t work - Cannot read property ‘get’ of undefined on the second event.

If i separate the events out into the create function by themselves, they both fire without issue. But i need one to be dependent on the other.

So when i store the 2nd event inside a function (outside of create) or conditional it fails.

Any advice will be hugely appreciated.

To fix the Cannot read property ‘get’ of undefined you need to make sure the event1 function is binded to same object, otherwise it does not know what you’re trying to do :

this.scene.get('GameSc').events.on('event1', function() {

triggered = true;

if(triggered){
    this.scene.get('GameSc').events.once('event2', this.handler);
}

}, this);// <--- add "this" as third argument to `events.on`

More info about the ‘on’ events function

thank you so much