Main Menu

I watched a tutorial on how to make a main menu when I try to run the game this pops up.

Which tutorial?

You should require GameManager properly, and it should be in the correct folder with the correct name.

2 Likes

Asatte Games Simple Main Menu

Then let’s ping @Asatte :slightly_smiling_face:

1 Like

Hi, if you are able to share the project file I’d love to help :slight_smile:
I assume you are from the comment of the tutorial video where no suggestion was working so seeing the project itself should make the problem easier to detect.
Or could you switch the ScneParent script with this to see if the error is only on one script or all of them have it:

--local GameManager = require("_Scripts.GameManager")

local function switch_to_scene(new_scene)
	if new_scene == "level" then
		--GameManager.current_level = new_scene
		msg.post("#menu", "unload")
		msg.post("#level", "load")
	elseif new_scene == "menu" then
		--GameManager.current_level = new_scene
		msg.post("#level", "unload")
		msg.post("#menu", "load")
	end
end

function init(self)
	msg.post(".", "acquire_input_focus")
	msg.post("#menu", "load")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("proxy_loaded") then
		msg.post(sender, "enable")
	end

	if message_id == hash("change_scene") and message.scene then
		switch_to_scene(message.scene)
	end

	--if message_id == hash("toggle_sound") then
	--	GameManager.is_sound_on = not GameManager.is_sound_on
	--end
end

function on_input(self, action_id, action)
	if action_id == hash("Tab") and action.released then
		--if GameManager.current_level == "level" then
		--	switch_to_scene("menu")
		--
	end
end

This is the correct version of the code in case you want to use it to check:

local GameManager = require("_Scripts.GameManager")

local function switch_to_scene(new_scene)
	if new_scene == "level" then
		GameManager.current_level = new_scene
		msg.post("#menu", "unload")
		msg.post("#level", "load")
	elseif new_scene == "menu" then
		GameManager.current_level = new_scene
		msg.post("#level", "unload")
		msg.post("#menu", "load")
	end
end

function init(self)
	msg.post(".", "acquire_input_focus")
	msg.post("#menu", "load")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("proxy_loaded") then
		msg.post(sender, "enable")
	end

	if message_id == hash("change_scene") and message.scene then
		switch_to_scene(message.scene)
	end

	if message_id == hash("toggle_sound") then
		GameManager.is_sound_on = not GameManager.is_sound_on
	end
end

function on_input(self, action_id, action)
	if action_id == hash("Tab") and action.released then
		if GameManager.current_level == "level" then
			switch_to_scene("menu")
		end
	end
end

My Project (3).zip (334.9 KB)

You can use your old script but make sure to use this:

local GameManager = require("assets._Scripts.GameManager")

for the require part. If you are not sure about the path next time, in the require type “GameManager” and it will provide the correct path option so you can select it.

Also, on the game.project → Graphics, select the Default Texture Min and Mag filter to nearest so the pixel art you use will look sharp.

Do you know why the buttons do not work when I run it?

Attach the gui script to the main menu GUI, then it should work but you are also missing the sound button so either add that as node too or remove the logic from the script so it will not throw any error


I am getting this error now

Cannot see the picture, can you paste the error here

ERROR:SCRIPT: assets/_Scripts/mainmenu.gui_script:13: Could not send message ‘change_scene’ from ‘mainmenu:/go#mainmenu’ to ‘main:/SceneParent’.
stack traceback:
[C]:-1: in function post
assets/_Scripts/mainmenu.gui_script:13: in function <assets/_Scripts/mainmenu.gui_script:10>

That’s unexpected because I do not have the same problem with your project. It can send the message and change the scene from the main menu to the game. Can you share the current version of the project or the menu and scene parent scripts?

1 Like

Actually it works now, thanks for your time you were very helpful!

1 Like

Glad to be of help, good luck with your game :slight_smile: