Monarch load screen during init black screen

I have a “controller” script that runs when the game starts and chooses what first screen to show.
All screens have preload=false
I wish to understand why If in the init method i directly call (MO is monarch lib):

function init(self)
    MO.show(CO.SCREEN_MAINMENU)
end

I get a black screen. Error i get is:
ERROR:SCRIPT: /monarch/monarch.lua:567: There is no screen registered with id hash: [MAINMENU]

if i load in response of a message, i get the screen properly shown:

function init(self)
    msg.post("#", CO.SCREEN_MAINMENU)
end

function on_message(self, message_id, message, sender)
    if message_id == hash(CO.SCREEN_MAINMENU) then
        MO.show(CO.SCREEN_MAINMENU)
    end
end

It works also if i preload the screen and then show in the callback

local function screenLoaded()
    MO.show(CO.SCREEN_MAINMENU)
end

function init(self)
    MO.preload(CO.SCREEN_MAINMENU, screenLoaded)
end

It is because the init of your controller script happens before the monarch screens have time to register.

If you do msg.post("#", CO.SCREEN_MAINMENU) you are “delaying” the MO.show(CO.SCREEN_MAINMENU) with 1 frame. As it takes one frame for messages to arrive. Preload is done with a message to the factory/proxy so the screens have times to register.

1 Like

It’s actually a quite simple explanation: The screen_proxy.script/screen_factory.script you attach to a proxy/factory to create a screen must run it’s init() functions first, before you can start loading any screens.