Godot Version
4.2
Question
Hi, I’m pretty new to Godot, and back whenever I originally implemented UI into the game I’ve been working on my implementation was truly awful. Now that I’m a little more competent I decided to make a system that was a little more foolproof. I have a singleton called Menu_Handler. The Menu_Handler has an Enum with states for each of the UI - states (Main menu, pause menu, settings menu, none, and inventory). I have functions for each of these states that handle what should happen if the user attempts to switch to one of these states while in their current state. from my testing it appears that calling these functions directly from the singleton works fine; however, whenever I try to call the function from one of the menus (ex. clicking the settings button in the main menu) the function is not executed. It doesn’t error or anything, it just simply does not trigger. I’ve tried adding breakpoints and the input to the button goes through completely fine, but the function is just simply not executed. I’ve autoloaded the singleton, and it autocompletes the function whenever I try to type it out so I don’t understand why the function cannot be called.
The code for the singleton is here:
extends Node
class_name Menu_Handler
enum MenuStates {MAIN, SETTINGS, PAUSE, INVENTORY, NONE}
var Menu := MenuStates.NONE
var prev_Menu := MenuStates.NONE
signal change_state(MenuState)
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
Menu = MenuStates.MAIN
prev_Menu = MenuStates.NONE
change_state.emit(Menu)
func _input(event: InputEvent) -> void:
if event.is_action_pressed("ESC"):
pause()
if event.is_action_pressed("Inventory"):
inventory()
func pause():
match Menu:
MenuStates.NONE:
prev_Menu = Menu
Menu = MenuStates.PAUSE
change_state.emit(Menu)
MenuStates.PAUSE:
prev_Menu = Menu
Menu = MenuStates.NONE
change_state.emit(Menu)
MenuStates.SETTINGS:
Menu = prev_Menu
prev_Menu = MenuStates.SETTINGS
change_state.emit(Menu)
MenuStates.INVENTORY:
prev_Menu = Menu
Menu = MenuStates.NONE
change_state.emit(Menu)
func inventory():
match Menu:
MenuStates.NONE:
prev_Menu = Menu
Menu = MenuStates.NONE
change_state.emit(Menu)
MenuStates.INVENTORY:
prev_Menu = MenuStates.INVENTORY
Menu = MenuStates.NONE
change_state.emit(Menu)
func settings():
match Menu:
MenuStates.SETTINGS:
print("exit settings")
Menu = prev_Menu
prev_Menu = MenuStates.SETTINGS
change_state.emit(Menu)
MenuStates.MAIN or MenuStates.PAUSE:
print("enter settings")
prev_Menu = Menu
Menu = MenuStates.SETTINGS
change_state.emit(Menu)
func main():
match Menu:
MenuStates.SETTINGS or MenuStates.PAUSE:
prev_Menu = Menu
Menu = MenuStates.MAIN
change_state.emit(Menu)
func none():
match Menu:
MenuStates.PAUSE or MenuStates.INVENTORY or MenuStates.MAIN:
prev_Menu = Menu
Menu = MenuStates.NONE
change_state.emit(Menu)
and the code for the main menu is here. The rest of the UI states have similar code.
extends Control
func _on_play_pressed():
MenuHandler.none()
func _on_settings_pressed():
MenuHandler.settings()
func _on_quit_pressed():
#probably save the game, Isaac
get_tree().quit()
func _on_menu_handler_change_state(MenuState: Variant) -> void:
if MenuState == MenuHandler.MenuStates.MAIN:
set_visible(true)
set_mouse_filter(Control.MOUSE_FILTER_STOP)
else:
set_visible(false)
set_mouse_filter(Control.MOUSE_FILTER_IGNORE)
sorry about that random comment btw, this is a collaborative project, and one of my companions is handling save/load in the game.
