How to reload a whole collection? (SOLVED)

Hello, I’m trying to reload a collection and has followed the documentation.
But the console keeps telling me:

ERROR:GAMESYS: The collection /main/level00.collectionc could not be disabled since it is not enabled. Message 'disable' sent from main:/go#script to main:/go#proxy.
ERROR:GAMESYS: The collection /main/level00.collectionc could not be finalized since it was never initialized. Message 'final' sent from main:/go#script to main:/go#proxy.
ERROR:GAMESYS: The collection /main/level00.collectionc could not be unloaded since it was never loaded. Message 'unload' sent from main:/go#script to main:/go#proxy.
ERROR:GAMESYS: The collection /main/level00.collectionc could not be disabled since it is not enabled. Message 'disable' sent from main:/go#script to main:/go#proxy.
ERROR:GAMESYS: The collection /main/level00.collectionc could not be finalized since it was never initialized. Message 'final' sent from main:/go#script to main:/go#proxy.

Before that it complained that our gameobject wasn’t loaded, but it was.
Right now I’m lost and in need of a nudge in the right direction.

Thanks

Could you post the code that deals with unloading the collection and any surrounding conditionals? It looks like it’s being called at points when it has not been loaded. Remember that loading is async so you need to wait for a proxy_loaded message before you can unload it again.

I tried to avoid that since everything is a mess, but this is the script which calls the proxy

if action_id == hash("restart") then
	local proxy = msg.url("#proxy")
	msg.post(proxy, "disable")
    msg.post(proxy, "final")
	msg.post(proxy, "unload")
	msg.post(proxy, "load")
	--msg.post(proxy, "unload")
end

I’ve tried different things but if I understand what you’re saying every GO needs a script that handles the call and answers with “proxy_loaded”. Or is it the proxy that returns it?

I mean everything loads but when I call it it says that it hasn’t been.

Sorry if I’m being too confusing :sweet_potato:

Was there a msg.post(proxy, “load”) somewhere else that was called before the restart was triggered? Otherwise I suspect that’s the reason for the error messages, the first time the proxy was simply not loaded yet. To make it conform a bit more to how the proxies are supposed to work, you could try:

-- first point of starting up the level
msg.post("#proxy", "load")

-- restart trigger
if action_id == hash("restart") and action.pressed then
	if self.loaded then
		msg.post("#proxy", "unload")
	end
end

-- dealing with load/unload
function on_message(self, message_id, message, sender)
	if message_id == hash("proxy_unloaded") then
	    -- just load it as soon as it's done unloading
		msg.post("#proxy", "load")
	elseif message_id == hash("proxy_loaded") then
		self.loaded = true
	end
end

Beware, I never ran this code so look out for typos. I’ll set up a test now and let you know if there are any errors.

I had forgotten to enable the proxy after loading it! This is the script I used to test reloading:

function init(self)
	msg.post(".", "acquire_input_focus")
	-- first point of starting up the level
	msg.post("#proxy", "load")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("proxy_unloaded") then
	    -- just load it as soon as it's done unloading
		msg.post("#proxy", "load")
	elseif message_id == hash("proxy_loaded") then
		self.loaded = true
		msg.post("#proxy", "enable")
	end
end


function on_input(self, action_id, action)
	-- restart trigger
	if action_id == hash("restart") and action.pressed then
		if self.loaded then
			msg.post("#proxy", "unload")
		end
	end
end

Yes, the script that sends “load” to the proxy gets a “proxy_loaded” message back from the proxy.

Note that “enable” implicitly sends “init” as well. If you need to you can send “init” and then “enable” explicitly.

1 Like

Hey there,
comlete newby here. Trying to understand this system, in what .script file did you put your code in the end?

Greetings,

  • Linus

What I usually do when it comes to loading and unloading collections is to have some kind of main/boot collection that is either set as the bootstrap collection in game.project or loaded at startup. This main collection has a bunch of collection proxies to menus, levels and other things that need to be loaded/unloaded depending on the state of the game. I usually also have some kind of controller script attached to the main collection and this controller script has the logic for loading and unloading collection proxies. I hope this answers your question.

I put together a real bare-bones example a while back. In the example I have a controller script and two collections being loaded and unloaded depending on game state:

http://britzl.github.io/publicexamples/menu_and_game/index.html

4 Likes

Thank you, thank you, thank you so much. I was having trouble trying to get the running frog tutorial game to restart. Your example project in github, helped immensely :smile:

2 Likes

Thanks! Happy to hear that! If you have suggestions for more example you’d like to see then please let me know.

4 Likes

Cheers, I might have to take you upon that offer at some point.

1 Like