Slow scene changes with Monarch

Hi - I am making a shoot 'em up, which means there’s a lot of objects being created with factories (bullets, enemy ships, explosions etc). If you play for 10 seconds then switching scenes (from game to main menu) is instant, but play for a couple of minutes and it can take 5 seconds or so to switch scene. I’m using Monarch to switch scenes.

My guess is that I’m not deleting the objects properly, or maybe I am doing something wrong with how I use Monarch.

Each time a component is finished with it gets deleted (go.delete()) but there are some objects I don’t delete when I switch scene (bullets/ enemies that are still moving. I assume that they get removed when the scene changes.

So my question is, what might I be doing wrong?

Is there a better way to delete game objects? Do I need to delete the individual elements that make up the object as well?

Do I need to delete the objects before I switch scene?

No, if you unload a collection proxy all objects created within the collection will get destroyed.

Are you using collection proxies or factories for the screens/scenes?

How are you testing the game? Build and run? Our do you target a running engine and reload/push new content to it?

What kind of limits have you set for sprites and game objects in game.project? And how many objects do you have active when you switch scene? (You can check this with the profiler)

Thanks for your reply

I am using Factories. I don’t know how the behaviour differs between Factories and Collection proxies, will have to read up on that.

Command+B, test, edit, then Command+B again

Sprite Max count is 256 but I don’t think I reach that.

I haven’t used the profiler before. I’ve taken a screenshot of the profiler (paused just before I switch screen) after it has switched back to the menu, but I don’t know what I’m looking at :slight_smile:

I also had this in the console (which I hadn’t seen before because I haven’t used the profiler before)


I’ve just realised go.delete( true ) deletes objects recursively which seems to have helped a bit, but it’s still slow as you can see above.

A collection loaded using a collection proxy creates a new contained “world” of game objects. These game objects will get destroyed together with the collection proxy when it is unloaded. The world will have it’s separate instances of the physics engine, and the game objects within one world can’t interact with game objects from another world (only through message passing). You can only have one instance of each collection proxy loaded at a time.

A collection factory on the other hand can be used multiple times to create instances of the game objects within the collection. The created game objects will live in the “world” which contained the collection factory. This could be either the bootstrap collection defined in game.project or within a loaded collection proxy. Collection factories are usually used when you have a group of game objects that belong together and you wish to create multiple instances of them. Game object instances created from a collection proxy can interact with any other game objects within the same world.

When using Monarch to load screens/scenes I’d recommend using collection proxies and only in special circumstances use collection factories.

It’s a snapshot of the current frame. There’s nothing that stands out. The profiler manual goes into quite a bit of detailed about the data shown by the profiler if you want to learn more.

Yes, this is interesting! It shows that there’s something that takes way too long! It indicates that it takes a very long time to destroy a collection proxy component. I thought you said you didn’t use any of those?

One thing I’d like you to try is to bundle a build of your game in release mode. Project->Bundle->macOS and then select Release in the drop down.

Is the game slow when switching scene even in release mode?

Thanks for explaining the collection factory and proxy differences. That makes total sense.

Tthe profiler is really interesting too - I will need to read up on it but I can see how it will be really helpful. I normally work with JavaScript so I’m used to doing everything through the console. Having a proper profiler is a revelation!

I may have misunderstood/ confused the original question. My scenes are being loaded as collection proxies, my game objects (enemies, bullets etc) are being created with factories.

I just tested this and the game didn’t switch scene at all. Not sure if that’s a bug with my code or related to this issue. I shall try again evening and see if I can reproduce it.

One other thing I have just tried is to stop the game spawning new enemies & bullets once the player has been destroyed. Previously I would run a timer for 2 seconds before loading tthe menu. The game would carry on as normal, but that means a lot of content would be created before the menu is loaded This should reduce the amount of components created that the player will never interact with.

Ah, I understand. This makes sense! If your game “scene” is loaded via a collection proxy all of it’s resources and spawned items will be removed by the engine when the collection proxy is unloaded. You should not have to manually delete anything. You can easily verify this with the profiler (check the Counters section as it shows the amount of game objects and components that exist in the sampled frame).

Hmm. Are you by any chance doing some string manipulation on the tostring version of a hash? In a release version the reverse lookups of hashes do not exist for performance reason.

Let me know how it goes!