[Monarch] monarch.back() calls init function of previous screen again

I am using monarch.back() to remove the screen and going back to the previous screen,

but when going back to the previous screen it calls monarch.init() again of the previous screen, this executes all the init code and some loading animation (which I am using for showing the screen) which looks wired especially for this scenario where I am just removing the top screen and showing back the previous screen.

Also, this is only happening when I am not marking any screen as a popup, as these are normal screens and not popups and also monarch.clear() is not working as expected on popups so I don’t want to use the screen as a popup.
how can I avoid monarch.init() on monarch.back()?

Hi there,

Check Monarch’s API for the back() function:

monarch.back([data], [callback])

The [data] argument is a Lua table. You can populate this table with a flag which tells the next screen to ignore the init() function. You can access this data table using Monarch’s data() function. For example:

-- Script #1
monarch.back({ ignore_init = true })

-- Script #2
function init(self)
    if monarch.data(this_screen_id).ignore_init then
    -- Continue with init() function

@klaytonkowalski offered a good solution, but here’s a bit of explanation as well:

The idea behind monarch is to handle a stack of screens (and popups). The screens can be just about anything, for instance a main menu, a game and a game over screen. In a small game it might be ok to have all of the screens loaded at the same time, but in a large game you may not have enough memory to keep everything loaded at the same time. Monarch handles all of this for you and loads and unloads screens add they are needed (ie visible at the top of the stack). And since screens are unloaded and loaded their lifecycle functions (init, final) are called each time.

1 Like

How can I keep all the screens in the stack active they will not call init again on screen become visible again, right? and monarch.clear() will clear unwanted screen in that case too.
also, I was thinking that focus_gained and focus_lost is created to understand screen is active again.

It sounds like you want to do things Monarch isn’t really designed for. If you want all screens loaded and visible and running code at all times you may try to set all of them to be popups. But you’re working against the system and might be better of creating something of your own… The whole idea of Monarch is that the screens should be part of the game engine lifecycle flow and when a screen is hidden the final() function will be called on the scripts of the screen and when a screen is shown the init() function should be run.

@britzl, I think I confused you with my last comment, apologies for that,

I got that all screens should be uninitialised when not on top and init again on coming foreground by the monarch.show() for better management of resources but I have some animation and sounds that I don’t want to play again as the screen just becomes visible because the player just removed top screen visually(from a player perspective). I will try the option suggested by @klaytonkowalski.

In other thought, a game I am working on is small and have a limited number of screens so I was thinking of another option to have all screens loaded at the same time, and I can activate them as per requirement.
but haven’t found any option other than marking them preloaded and with preload enabled to specific screen… however monarch.init() is still called one screen come on top of the stack, am I doing something wrong or misunderstood preload option?

The purpose of the preload option is to load any assets required by the screen before it is used. This will remove any loading delay as you switch to a screen that has the content preloaded. The init() function will still be called though.