Scene event after create has completed

Hi guys,

I posed this question on Slack as well but thought I’d try my hand here, too.

I’d like to trigger an event after a potentially long running Scene initialization. Is there an event for when after Scene#create has finished? I know there’s Phaser.Scenes.Events.CREATE_EVENT for something similar, but I think that may be for when it’s called and doesn’t account for execution time.

TIA

Are you needing it right after create() finishes? Is it something you can do once in update(), because it runs after create().

Maybe knowing the context of the problem would help.

There is also READY and PREUPDATE. Maybe one of those could do what you are looking for.

@Jackolantern I want to fire an event, once, after create has completed finished. Ideally I’d like to avoid doing something to hack around it like running it in update.

I’m working on a scene transition and I don’t want the “in” transition to trigger until the scene has set itself up. In other words, I’d like to show a blank screen until the scene is ready to go and then animate in. The problem is that create may take some arbitrary amount of time (ie, perhaps spawning loads of enemies or generating a large map).

Hmmm…maybe the READY event would work. I do get not wanting to put it in update(), but if READY doesn’t work, maybe you could use PREUPDATE. I don’t know if that would keep firing with each update, but you could stop listening at the end of the event handler so it would effectively only run once. Maybe not extremely ideal, but it also doesn’t feel super hacky to me.

It would be nice if we had (lifecycleEvent)Complete events for each lifecycle stage, like initComplete, preloadComplete, createComplete, etc., but it doesn’t seem that anything exactly like that exists.

Ahhh, I think I’ve got it!

this.events.once('preupdate', function() {
    console.log('gameScene started!');
});

I actually didn’t know it supported once() until I went digging through the EventEmitter documentation. That is about the cleanest solution I can find, short of (blank)Complete events being added.

Turns out that the create event does work correctly. Through some discussion on Slack this morning, we found out that my issue is that events don’t wait for async operations (this never crossed my mind).

I’m sure your solution would work in most scenarios, but I’ll need to rethink my approach because I need to do some scene setup with some data fetched from an API.

Thank you for taking the time to help me with this, @Jackolantern! It is very much appreciated.

Ahhhh, yes, it will not wait for an async operation to complete. Can you wrap the async operation in a promise and put the code that would be in your create event handler inside a function in a THEN clause? That way, even when create goes out of scope no matter when your async operation completes, your code can respond to it? Since you are setting up a transition, could you hold the screen black or whatever the mid-point of your transition is until your async operation completes? You could keep a flag that would cause update to not begin executing until that promise resolves and you set the flag to allow update to begin running. That would prevent your logic from beginning without the data from the async call. I don’t know of a way to actually keep create() from completing and beginning update() without using flags.

Anyway, best of luck on it! And no problem at all. I am learning stuff as well when I look into issues with parts of Phaser I haven’t worked with a ton yet.

Oh wait…what kind of data are you loading asynchronously? Did you know Phaser has a JSON loader? You can actually point its preloader at a REST endpoint and get the response as JSON (just be sure your API is looking for a GET). The preload() method will not continue on until the resources are loaded so it will wait for the async call to complete. If you need to do some kind of setup before you can make the call, is it something you could do in a boot scene before your game scene gets loaded? And any parameters your API needs could be passed as a querystring through the JSON preloader.

It’s funny you mention using the JSON file loader because I actually wrote an article about doing basically that. I hadn’t considered using it here.

I think what’s going on here is that I’ve over-engineered what I’m working on. I’m trying to create a reusable piece of code to handle all sorts of implementation details in whatever configuration. I think I’m just going to have to outline these limitations and admit that it might not work in every situation.

The more I’m thinking about it the more I’m convinced that this is the way to go.

I have a problem with doing the same thing. My day job is making enterprise software in .NET, and those same kinds of patterns creep into my games even though they are probably the wrong abstraction. The complexity starts exploding and before I know it, I have 5 projects in my solution, 40 folders with piles of classes in them and I don’t even have the login system completed lol.

I recently ran a project that got out of control because I was way over-complicating it. I am just about to start over and try to simplify and streamline everything, and only expand things out as I actually need the feature or run into performance issues when testing.

Well, best of luck to you! It seems like you are on the right trail! :slight_smile:

EDIT: That is a nice tutorial, by the way! I think I will read that :smiley:

Sounds like you’re a typical developer! :stuck_out_tongue: I think we all have issues with this. I’ve been working on the same project for over 5 years (depressing now that I think of it), just refactoring it over and over again. At this point I think I just do it to learn and try something new, with tiny hints at progress along the way.

Thank you for the compliment! I hope you enjoy reading it. I’m working on a new one at the moment. This thread is part of my research for it, actually.

1 Like

Nice! I will definitely keep an eye out for it. Do all of your tutorials and articles go up on that site?

Yep! I’ve only got the one so far. I recently decided that if I can’t finish a game I can at least contribute back to the community this way. I’ve been working on a plugin or two as well.

Oh, those looks nice! I will bookmark them and keep them in my toolbox. Thanks! :wink:

Great! Would love any feedback you have if you ever find a use for them.