How to restart a level from main.collection?

I would like to ask for help to make a simple reload of the current screen.

I’m doing the snake game tutorial on the site and even after reading the documentation and searching here on the forum it was not clear to me how to reload.

on the godot engine just make a node.reload_current_scene ()

here is a complex system of proxies that have not yet figured out how it works could someone please help me. I’ve been stuck here for a few days.

I would like an example with code here someone from the forum said that just an unload and load but I didn’t understand how hahaha

Thanks for the personal help the engine is amazing!

1 Like

have you read this post:

Also this one which he has a second script he uses to load and unload scenes:

One thing about loading and unloading proxies is that you have to use the same object that loaded them the first time. This is why we use a a separate script called loader or controller usually, that we use to load and unload all the scenes. When you try and load and unload a scene from itself youll usually get a “scene already loaded” error or some such.

You can try these steps with an empty project.

  1. Create the loader.script like this:
local hashes = {
	did_init = hash 'did_init',
	restart = hash 'restart'
	load = hash 'load',
	unload = hash 'unload'
	proxy_loaded = hash 'proxy_loaded',
	proxy_unloaded = hash 'proxy_unloaded',
	enable = hash 'enable',
	disable = hash 'disable',
	init = hash 'init',
	final = hash 'final',
}

local urls = {
	this = msg.url '.',
	game_proxy = msg.url '#game_proxy' -- id of your proxy component
}

function init(self)
	msg.post(urls.this, hashes.did_init)
end

function on_message(self, message_id, message, sender)
	if message_id == hashes.did_init or message_id == hashes.proxy_unloaded then
		-- start proxy loading
		msg.post(urls.game_proxy, hashes.load)
	elseif message_id == hashes.proxy_loaded then
		-- init and enable a loaded proxy
		msg.post(sender, hashes.init)
		msg.post(sender, hashes.enable)
	elseif message_id == hashes.restart then
		-- disable, deinit and unload proxy
		msg.post(urls.game_proxy, hashes.disable)
		msg.post(urls.game_proxy, hashes.final)
		msg.post(urls.game_proxy, hashes.unload)
	end
end
  1. Change the id of your main collection from ‘default’ to ‘main’.
  2. Add the ‘root’ gameobject to the main.collection.
  3. Attach the loader.script component to this object.
  4. Attach the ‘game_proxy’ collection proxy component to this object.
  5. Create your game collection that you want to reload and set it in the collection proxy properties.
  6. When you want to reload your game collection just post ‘restart’ message to your loader script:
local hashes = {
	restart = hash 'restart'
}

local urls = {
	-- we need a full url with main collection id
	-- beacuse we are in the other proxy world.
	loader = msg.url 'main:/root#loader'
}

...

msg.post(urls.loader, hashes.restart)

Hey just wanna post a simpler way that I’ve seen here at this forum:

local function show(self, proxy) -- method to unload and load proxy
   if self.current_proxy then
       msg.post(self.current_proxy, "unload")
       self.current_proxy = nil
   end
   msg.post(proxy, "async_load")
 end

function init(self)
    self.current_proxy = nil
end

function on_message(self, message_id, message, sender)
   if message_id == hash("restart") then -- restart
      show(self, self.current_proxy)
   elseif message_id == hash("next_level") then -- example how to load next levels
      show(self, "#level_" .. data.current_level)  then -- data.lua stores your levels
   elseif message_id == hash("proxy_loaded") then
      self.current_proxy = sender
      msg.post(sender, "enable")
   end
end

I think this is really great to understand.
good luck!

1 Like

Yes, collection proxies are a bit more complex but also very powerful. You can load and unload the collection referenced by a collection. We do not have a reload command though, although that might be a nice addition to our API.

4 Likes