Yesterday, I started creating a menu and got stuck on an error when trying to send a message to controller.script:
ERROR:GAMEOBJECT: Instance '/controller' could not be found when dispatching message 'show_setting' sent from main:/menu#main-menu
Most likely, I missed something or didn’t read something carefully when creating the menu. For reference, I used the menu_and_game and MainMenu projects.
Here is the controller.script code:
function init(self)
msg.post(".", "acquire_input_focus")
msg.post("#menu_proxy", "load")
end
function final(self)
msg.post(".", "release_input_focus")
end
function on_message(self, message_id, message, sender)
print(message_id)
if message_id == hash("proxy_loaded") then
msg.post(sender, "init")
msg.post(sender, "enable")
end
if message_id == hash("show_game") then
msg.post("#game_proxy", "load")
msg.post("#menu_proxy", "final")
msg.post("#menu_proxy", "disable")
msg.post("#menu_proxy", "unload")
elseif message_id == hash("show_newgame") then
msg.post("#game_proxy", "load")
msg.post("#menu_proxy", "final")
msg.post("#menu_proxy", "disable")
msg.post("#menu_proxy", "unload")
elseif message_id == hash("show_load") then
msg.post("#load_proxy", "load")
msg.post("#menu_proxy", "final")
msg.post("#menu_proxy", "disable")
msg.post("#menu_proxy", "unload")
elseif message_id == hash("show_setting") then
msg.post("#setting_proxy", "load")
msg.post("#menu_proxy", "final")
msg.post("#menu_proxy", "disable")
msg.post("#menu_proxy", "unload")
elseif message_id == hash("show_main-menu_setting") then
msg.post("#menu_proxy", "load")
msg.post("#setting_proxy", "final")
msg.post("#setting_proxy", "disable")
msg.post("#setting_proxy", "unload")
elseif message_id == hash("show_main-menu_load") then
msg.post("#menu_proxy", "load")
msg.post("#load_proxy", "final")
msg.post("#load_proxy", "disable")
msg.post("#load_proxy", "unload")
end
end
Here is the main-menu.gui_script code:
function init(self)
msg.post(".", "acquire_input_focus")
sound.play("/music#sound", { gain = 1.0, pan = 0 })
self.continue_button = gui.get_node("continue_button")
self.newgame_button = gui.get_node("new-game_button")
self.load_button = gui.get_node("load_button")
self.setting_button = gui.get_node("setting_button")
self.exit_button = gui.get_node("exit_button")
end
function final(self)
msg.post(".", "release_input_focus")
end
function on_input(self, action_id, action)
if action_id == hash("touch") and action.pressed then
if gui.pick_node(self.continue_button, action.x, action.y) then
msg.post("/controller#script", "show_game")
end
if gui.pick_node(self.newgame_button, action.x, action.y) then
msg.post("/controller#script", "show_newgame")
end
if gui.pick_node(self.load_button, action.x, action.y) then
msg.post("/controller#script", "show_load")
end
if gui.pick_node(self.setting_button, action.x, action.y) then
msg.post("/controller#script", "show_setting")
end
if gui.pick_node(self.exit_button, action.x, action.y) then
os.exit()
end
end
end
Here is the controller.collections structure:
Here is the project file structure:
Just Try Create Game
├───assets
│ ├───font
│ │ ├───sfx
│ │ ├───Capture_it.ttf
│ │ ├───main-menu.font
│ │ ├───ThinFourDownThreeCrazyPink-Regular.ttf
│ │ └───title-game.font
│ ├───images
│ │ └───menu-background.jpg
│ └───music
│ └───main-theme.ogg
├───atlas
│ └───main-menu.atlas
├───collections
│ ├───menu
│ │ ├───game.collection
│ │ ├───load.collection
│ │ ├───main-menu.collection
│ │ └───setting.collection
│ └controller.collection
├───gui
│ ├───load
│ │ ├───load.gui
│ │ └───load.gui_script
│ ├───main-menu
│ │ ├───main-menu.gui
│ │ └───main-menu.gui_script
│ └───setting
│ ├───setting.gui
│ └───setting.gui_script
├───input
│ └───game.input_binding
├───objects
├───scripts
│ └───controller.script
├───tileset
└───game.project
