How to create a "Main menu" and manage "Level menu"?

Hi,

I tried again and again to understand how to create a main menu…

I understood that it deals with Gui File et Gui Script File but I can’t make it work, it doesn’t catch any touch action even if I added in my input and I don’t really understand how I’ll manage my main menu and my level menu.

A little help there please ? :smiley:

Hey Prawn! I’m in no way a developer, but have you checked out the Main menu code sample? You might find some inspiration there.

Hey Axel, thanks for the link, I already checked it before asking on the forum :wink: and it doesn’t really explain how to create a main menu from the beginning

Gotcha! I’ll get out of here, and hope someone more competent than myself will answer shortly.

1 Like

it doesn’t catch any touch action even if I added in my input

have you send message to acquire input focus?

check this out
http://www.defold.com/doc/input#_acquiring_and_releasing_input_focus

1 Like

Some of this might help @Prawn:

Input
Each game object that want to receive input must acquire input by posting a message to itself:

msg.post(".", "acquire_input_focus")

You should also understand how input is propagated when you work with collections and collection proxies. Read up on this here:

http://www.defold.com/doc/input#anchor-iacp

"Screen management"
So, in a game where you wish to have a number of screens/menus then one way to achieve this is to put each screen/menu in it’s own GUI file. Each GUI file points to a GUI script file with the logic for that GUI. All of this is described in detail in the documentation on the GUI component:

http://www.defold.com/doc/gui

Now, one way to manage these different GUIs is to have a game object with a controller script that takes care of showing and hiding GUIs depending on the state of your game. You can post messages to this controller script to show different GUIs and let the controller script in turn post to the different GUIs of your game to show/hide them. An example:

controller.script:

local function hide_screen(url)
	if url then
		msg.post(url, "hide")
	end
end

-- show a screen and hide the previously shown screen if one existed
local function show_screen(self, url, message)
	hide_screen(self.current_screen)
	self.current_screen = url
	msg.post(self.current_screen, "show", message or {})
end

-- start by hiding all guis and show the main menu	
function init(self)
	hide_screen("main_menu")
	hide_screen("level_menu")
	show_screen(self, "main_menu")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("show_main_menu") then
		show_screen(self, msg.url("main_menu"))
	elseif message_id == hash("show_level_menu") then
		-- pass along additional data such as which level to play to the level menu
		show_screen(self, msg.url("level_menu"), message)
	end
end

main_menu.gui_script:

function final(self)
	msg.post(".", "release_input_focus")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("show") then
		msg.post(".", "enable")
		msg.post(".", "acquire_input_focus")
	elseif message_id == hash("hide") then
		msg.post(".", "disable")
		msg.post(".", "release_input_focus")
	end
end

function on_input(self, action_id, action)
	if action.released and gui.pick_node(gui.get_node("play_level"), action.x, action.y) then
		msg.post("controller", "show_level_menu", { level_id = 1234 })
	end
end
5 Likes

Thanks a lot for all your answers. It works well !! :grin:

But I have one last question when I’m on my level menu how to manage all my “levels board” where the player can play :smiley:

:heart: britzl: thanks for your code sample, it helps me a lot. :+1:

Do you mean how to manage the actual levels? If so then that depends quite a bit on the game, but it might be a good idea to have each game level in it’s own collection and load/unload it using a collection proxy.

Yep I heard about using collection proxies, I will take a look. thanks :smile:

It’s discussed a bit more here: Level switch (change level)

1 Like