[SOLVED] Can't load minigames after one another through collection proxies

I’m going off of the Mini Game Party in order to try doing something similar to Warioware. However I’d like make it so that the program loads the minigames before hand, then loads them one at a time, because when debugging, each of the minigames are loading right after one another. Another problem is it’s actually not loading anything, and there’s no message giving me errors.

local MINIGAMES = {
	{ proxy = "#insert_book_code_proxy", script = "insert_book_code:/game#script" } ,
	{ proxy = "#shelving_proxy", script = "shelving:/game#script" },
	{ proxy = "#where_to_find_book_proxy", script = "where_to_find_book:/game#script" }
}

local score = 0

local function shuffle(t)
	for n=1,#t do
		local k = math.random(#t)
		t[n], t[k] = t[k], t[n]
	end
	return t
end

local function minigame_load()
	MINIGAMES = shuffle(MINIGAMES)
	for _, minigame in ipairs(MINIGAMES) do
		msg.post(minigame.proxy, "load")
	end
end

function init(self)
	minigame_load()
end

-- nested function for loading and unloading?

function on_message(self, message_id, message, sender)
	if message_id == hash("game_over") then
		msg.post(minigame.proxy, "unload")
		minigame_load()
	end
end

Also I’ve probably have to store minigame.proxy in a variable due to scope. Thanks in advance.

Not sure I understand this, but why do you want to first load all of them and then one at a time?

Ah, my bad for being unclear, I’m just trying to load the minigames one at a time, currently they’re all loading ‘simultaneously’, right after one another, instead of one being loaded, then unloaded, then the next one being loaded, and then unloaded, and so on. Also the collections just aren’t loading for some reason, even though there’s no error or anything.

Have you studied the collection proxy example on the website? It shows exactly how it is supposed to be implemented.

I have read Proxy which is what I’m assuming you’re talking about, I just wasn’t sure about how to adapt it to my situation. Thinking about it though, it seems pretty obvious now, let’s see if I can try doing that somehow. Thanks britzl, and sorry, I seem to be asking a lot of questions ever since I started. :sweat_smile:

From what I’m thinking, I’m probably going to have to add to the table another property like message_id_to_pass.

local MINIGAMES = {
	{ proxy = "#insert_book_code_proxy", script = "insert_book_code:/game#script", message_id_to_pass="insert_book_code_proxy" } ,
	{ proxy = "#shelving_proxy", script = "shelving:/game#script",  message_id_to_pass="shelving_proxy"},
	{ proxy = "#where_to_find_book_proxy", script = "where_to_find_book:/game#script", message_id_to_pass="where_to_find_book_proxy" }
}

local score = 0

local function shuffle(t)
	for n=1,#t do
		local k = math.random(#t)
		t[n], t[k] = t[k], t[n]
	end
	return t
end

local function minigame_load()
	MINIGAMES = shuffle(MINIGAMES)
	for _, minigame in ipairs(MINIGAMES) do
		msg.post("#", minigame.message_id_to_pass)
	end
end
-- make this and minigame_load into one somehow?
local function show(self, proxy)
	self.current_proxy = proxy
	msg.post(proxy, "async_load")

function init(self)
	self.current_proxy = nil
	minigame_load()
end

-- nested function for loading and unloading?

function on_message(self, message_id, message, sender)
	if message_id == hash("show_insert_book_code_proxy") then -- <4>
		show(self, "#insert_book_code_proxy")
	elseif message_id == hash("shelving_proxy") then
		show(self, "#shelving_proxy")
	elseif message_id == hash("where_to_find_book_proxy") then
		show(self, "#where_to_find_book_proxy")
	elseif message_id == hash("proxy_loaded") then -- <9>
		self.current_proxy = sender -- <10>
		msg.post(sender, "enable") -- <11>
	elseif message_id == hash("proxy_unloaded") then
		print("Unloaded", sender)
	end
	
	if message_id == hash("game_over") then
		msg.post(self.current_proxy, "unload")
		minigame_load()
	end
end

But this will load all of them at the same time. Shouldn’t you only take one value from MINIGAMES and load that?

2 Likes

:thinking: Yes, but then how would I know what minigame to load next afterward, that’s what I’m trying to think, because this isn’t done from a menu where you select the level, it’s like a queue. I feel like there should be a break somehow. :man_facepalming:, wait, I could probably use something like a function called find for arrays (plop in index to get corresponding element, once loaded and unloaded, then remove that element so it can’t be repeated) but with tables. Doing a bit of research by reading Search for an item in a Lua list - Stack Overflow, I might have to do something like a set, although I might be overengineering or overcomplicating this.

Create the list, store it in a variable on self, shuffle the list once and then each time use

local minigame_to_load = table.remove(self.minigames)

To get the next game to load and at the same time remove it from the list.

Thank you so much, it’s mostly working as intended, is there any way to unload the main collection though? Would it pose problems to make a collection proxy of the collection it’s inside of?

local MINIGAMES = {
	{ proxy = "#insert_book_code_proxy"} ,
	{ proxy = "#shelving_proxy"},
	{ proxy = "#where_to_find_book_proxy"}
}

local function shuffle(t)
	for n=1,#t do
		local k = math.random(#t)
		t[n], t[k] = t[k], t[n]
	end
	return t
end

local function minigame_load(self)
	local minigame_to_load = table.remove(self.minigames)
	msg.post("#", "collection_proxy_to_load", {proxy  = minigame_to_load.proxy})
end

local function show(self, proxy)
	msg.post(self.current_proxy , "unload")
	self.current_proxy = proxy
	msg.post(self.current_proxy, "async_load")
end

function init(self)
	msg.post(".", "acquire_input_focus")
	self.current_proxy = "#main"
	self.minigames = shuffle(MINIGAMES)
	minigame_load(self)
end

local score = 0

function on_message(self, message_id, message, sender)
	if message_id == hash("collection_proxy_to_load") then -- <4>
		show(self, message.proxy)
	elseif message_id == hash("proxy_loaded") then -- <9>
		self.current_proxy = sender -- <10>
		msg.post(sender, "enable") -- <11>
	elseif message_id == hash("proxy_unloaded") then
		print("Unloaded", sender)
	end
	
	if message_id == hash("game_over") then
		msg.post(self.current_proxy, "unload")
		score = score + message.score
		minigame_load(self)
	end
end

You can’t unload the main collection.

1 Like

You need one “main” collection which has many proxies inside, and the code for loading and unloading those proxies. Apart from that, the main collection can be totally empty (i.e. no sprites, no sounds, etc) so the player doesn’t see it or hear it. That way, you can get the main proxy to load a “start screen” collection proxy (where players select a mini game), then when a player has selected a mini game, unload the start screen, and load the mini game.

1 Like

Thanks guys, thanks for popping in too @88.josh and explaining.