I am trying to create a game that has a how to play screen that can be accessed from the main menu but I can’t make my program go back to the main menu from the how to play screen. How can I achieve this?
One common way of implementing multiple “screens” or “scenes” is to use multiple collections, one per screen, and load and unload them through collection proxies. Example:
You can also use a screen manager such as Monarch:
I have tried to do this without success, how can I send you my project? to take a look at?
You can probably share it here, and someone can help you.
These are my scripts for switching between the ‘main menu’ and the ‘how to play’ screen and the ‘loader’ that actually switches the guis:
So far I am able to start the game from the menu, go to the how to play screen from the main menu and exit the game from the main menu. But when I try to go back to the main menu from the how to play screen, it doesn’t do anything.
I see that you are sending “load” and “enable” at the same time:
msg.post("#foo", "load")
msg.post("#foo", "enable")
This is not how you are supposed to do it. Take a look at the example I shared:
In the example we wait for the “proxy_loaded” message before sending the “enable” message.
But I get the error and I don’t know why.
If it’s easier, I can send over my entire project
Sure, zip and share it here. Exclude build, .git and .internal folders if you have them in your project.
How to I zip files?
Depends on your operating system, but usually there’s an option when you right click on the project folder from your system file browser.
In your loader.script
on_message
function you’re still not use the correct URLs to the proxy components. This is what it looks like in the project you sent me:
function on_message(self, message_id, message, sender)
if message_id == hash("show_menu") then
show(self, "#mainmenu")
elseif message_id == hash("play") then
show(self, "main")
elseif message_id == hash("howtoplay") then
show(self, "howtoplay")
Notice that there is no #
sign in front of “main” and “howtoplay”.
The message you are sending is to the proxy collection component, prefixed by the #
. Please read the following manual on how the addressing system works in Defold to learn more:
Thank you for your help, however I can’t go from the ‘how to play’ screen and back to the ‘main menu’. But I don’t get any errors so I don’t know what wrong. Here is my updated project with the changes I have implemented:
test.zip (147.7 KB)
In your howtoplay.gui_script:
function on_input(self, action_id, action)
if action_id == hash("click") and action.released then
if gui.pick_node(gui.get_node("return"), action.x, action.y) then
msg.post("loader:/go#loader", "return")
end
end
end
You send a “return” message to the loader script. And here’s your loader.script
:
function on_message(self, message_id, message, sender)
if message_id == hash("show_menu") then
show(self, "#mainmenu")
elseif message_id == hash("play") then
show(self, "#main")
elseif message_id == hash("howtoplay") then
show(self, "#howtoplay")
elseif message_id == hash("proxy_loaded") then
self.current_proxy = sender
msg.post(sender, "enable")
elseif message_id == hash("proxy_unloaded") then
print("Unloaded", sender)
end
end
Note that there is no handler for the “return” message. You should send a “show_menu” message instead.
I recommend that you learn how to use the debugger. Adding a breakpoint in on the on_message function it would be immediately obvious to you what’s going on!
Thank you for all of your help, it now works exactly how I intended it to.