Restore game state (sprites)

I have this game where the user places spheres on a board, each sphere is a game object with an attached sprite.
So far so good until i decided to add a settings screen.
To manage the screens i use Monarch and each screen is a collection.

What happens when i switch to the settings screen is that the game screen is… destroyed/unloaded? Whatever happens, each sprite is destroyed (i can see the spritec count in profiler decrease)

In the backend i keep a table with sprite references and another table with the actual game state (position occupied, which player, etc)

What is the correct way to handle the screen change?
Should i use anoher approach that avoids destroying all my sprites or this is good?

What is the best approach to restore the state?
Simply cycle on my state variables and recreate the sprites or there’s a more elegant way to do all this?

Thanks

Monarch will unload the previous screen if it is a collection proxy or delete the game objects if it is a collection factory.

If you track the game state then recreate the presentation of the game state (game objects etc) when the game screen is loaded.

Another option would be to select the Preload checkbox for the screen. With the Preload checkbox enabled the screen will be loaded in the background but not enabled until it is supposed to be shown and when the screen is hidden it will be disabled but not unloaded.

ok, i checked it and i’m using collection proxies.

Setting the preload flag seems to work well enough and i don’t have to recreate anything as everything remains aligned when i go back from settings to game.

Only drawback is that after going back from settings to game screen, rendercam is somewhat screwed… proportions are correct but it’s zoomed in.
To avoid this i have to set the preload flag also on the settings screen…uhm…

Now i’m wondering if defold-orthographic would have been more “compatible”

Why not leave the game as it is, pause it by setting the timestep if required and use the popup option in monarch to show the settings screen on top.

I just noticed you can set the timestep in monarch also.

1 Like

uh…maybe…i have no idea of this “timestep” you’re talking about,i’ll take a look in the docs :sweat_smile:

Are you using the “Ortho Scale” setting on your Rendercam camera? (as something other than 1) Do you have a camera for each screen, or just one?

I have a camera for each screen collection (i had to do it actually otherwise switching to the settings screen would not display correctly)

They are both setup like this:

1 Like

mmmm… maybe this log gives some hints…it has been always there (even before monarch and screens) but i’ve ignored it since everything was fine

DEBUG:SCRIPT: NOTE: rendercam - No active camera found this frame…using fallback camera. There will be no more warnings about this.

1 Like

Yeah, that message will show up if the render script ever updates without an active camera. In this case I’m assuming your bootstrap collection doesn’t have a camera and you load everything else asynchronously with proxies. It’s no problem.

Hmm, not sure what is going wrong. I’ve never used Monarch myself. Any chance you can zip up your project and let me take a look at it?

[Edit] OK, I think I found the issue. It’s just that, with “Preloaded” screens, the correct camera is not being re-activated when you switch back to that screen. Since you’re not initializing the collection again, there’s nothing to tell that camera to become active again, so it’s actually using the fallback camera. (Repeated warning messages would have been useful in this case…) So you just need to get the ID of that camera and use rendercam.activate_camera(id) to activate it again when you switch to that screen.

[Edit2] If most of your screens are just for different GUIs, you probably don’t need a separate camera for each one, since the GUI will be rendered in screen-space anyway. You might only need a camera for your main gameplay screen, and not have to switch cameras at all. Or maybe have a camera in your bootstrap collection for the menus and only one other camera in the gameplay collection.

Ok, i’ve played around a bit.

Without preload, i have to have a camera on both game and settings screens, otherwise i get the settings stretched.
But to avoid recreating all the setup for the game at each switch, i want to use preload and in this case i have to have 2 cameras and both screens preloaded otherwise settings gets stretched again.

But as you suggested, i tried to remove the settings screen camera and set preload to true on the main screen and everything seems to work nicely this way.
I assume this happens because with preload true the camera is not unloaded and remains active.

Thanks to everyone

1 Like

When you navigate from a screen that is flagged with “preload” it will not be unloaded, but the collection proxy should be disabled. I’m not sure how RenderCam decides if a camera is active or not? Is that something you do manually?

No, i just put the rendercam script in the game collection

It happens on the camera’s init() if it’s “Active” property is checked, or manually with rendercam.activate_camera().

So if the collection with a camera is flagged with “preload”, the camera will be activated only the first time it is loaded. If you delete the active camera it uses some fallback camera settings (which is what’s happening when marcopar is seeing a zoomed in view).

I guess I didn’t think about this application of the usual “enable” and “disable” messages. Probably something I should add…