How to call a function through the timer?

I’m looking to make a splash screen, I have 4 collections, a loader collection (which has collection proxies for the other 3), a splashscreen collection, a mainmenu collection, and a main collection (where the game takes place). In my loader.script I want to be able to call my function “unload_splash” from inside my function “load_splash”, the idea is that load_splash’s code exectues, then starts a timer, and when that time is up it will unload. If anyone has any idea of a better way to do a splash screen so its more fading in then please let me know.

Many thanks in advance :slight_smile:

Try:

timer.delay([time in seconds], false, function(self)
   -- Code to run when the timer ends
end)
1 Like

So this is the code I’m using in my loader.script:

local function unload_splash(self)
	msg.post("go#splashscreen", "unload")
end

local function load_splash(self)
	msg.post("go#splashscreen", "load")
	msg.post("go#splashscreen", "enable")
	timer.delay(5, false, function(self)
		unload_splash(self)
		load_menu(self)
	end)
end

local function load_menu(self)
	msg.post("go#mainmenu", "load")
	msg.post("go#mainmenu", "enable")
	
end

local function unload_menu(self)
	msg.post("go#mainmenu", "unload")
end

local function load_main(self)
	msg.post("go#main", "load")
	msg.post("go#main", "enable")
end

function init(self)
	msg.post(".", "acquire_input_focus")
	load_splash(self)
end

function on_message(self, message_id, message, sender)
	if message_id == hash("start_game") then
		unload_menu(self)
		load_main(self)
	end	
end

I now see the splash screen show up, it goes away after 5 seconds as i set it to, but then its just a black screen, when it should be calling load_menu and loading up the main menu, it isnt for some reason, any ideas?

The main issue here is the disconnect between your “load” and “enable” which suggests you should rearrange the code a bit.

Call “load”, then wait for the message “proxy_loaded” and only after that point should you initialise the collection.

function on_message(self, message_id, message, sender)
    if message_id == hash("proxy_loaded") then
        -- New world is loaded. Init and enable it.
        msg.post(sender, "init")
        msg.post(sender, "enable")
        ...
    end
end

I think even the shutdown of the splash screen shouldn’t be scheduled until after it was successfully loaded. (it doesn’t matter in the end result, but it’s a bit confusing)

2 Likes

You need to rearrange your code a bit. Local functions need to be defined before they can be called. You can’t have a line calling load_menu() above the line where load_menu() is defined.

You could take a look at a screen manager such as Monarch which would take care of all the loading and unloading for you.

1 Like