I've put in methods that will cause a sprite to move next to each menu option as it's hovered over. I then have sibling trees that I disable and re-enable to swap between menus. The method that moves the sprite works in one tree, but not the other, despite having nearly identical scripts.
My node tree:
/menu_cont
/menu_cont/main_cont
/menu_cont/main_cont/play_button
/menu_cont/main_cont/create_button
/menu_cont/main_cont/settings_button
/menu_cont/main_cont/exit_button
/menu_cont/main_cont/menu_sprite
/menu_cont/settings_cont
/menu_cont/settings_cont/s_keybindings_button
/menu_cont/settings_cont/s_back_button
/menu_cont/settings_cont/settings_sprite
The working version is like so:
In each button, for example "/menu_cont/main_cont/settings_button" I have:
func _on_settings_button_mouse_enter():
get_node("../menu_sprite").move_selector(2)
In "/menu_cont/main_cont/menu_sprite" I have:
var selected # {0,1,2,3} = {play, create, settings, quit}
# x,y of the sprite
var selected_name = ["play_button","create_button","settings_button","exit_button"]
var num_buttons = 4
var sprite_offset = Vector2(-112,64)
func _ready():
move_selector(0)
set_process_input(true)
func move_selector(v):
if str(v) == "down":
if selected >= (num_buttons - 1):
selected = 0
else:
selected += 1
elif str(v) == "up":
if selected <= 0:
selected = (num_buttons - 1)
else:
selected -= 1
else:
selected = v
# move sprite
set_pos(Vector2( get_node("../" + selected_name[selected]).get_pos().x + sprite_offset.x , get_node("../" + selected_name[selected]).get_pos().y + sprite_offset.y))
func option_accept():
get_node("../" + selected_name[selected]).accept()
func _input(event):
# keyboard
if event.is_action_pressed("ui_down"):
move_selector("down")
elif event.is_action_pressed("ui_up"):
move_selector("up")
elif event.is_action_pressed("ui_accept"):
option_accept()
The version that doesn't work is in my settings menu. I have "/menu_cont/settings_cont/s_back_button":
func _on_s_back_button_mouse_enter():
get_node("../settings_sprite").move_selector(1)
And in "/menu_cont/settings_cont/settings_sprite" I have:
var selected # {0,1,2,3} = {play, create, settings, quit}
# x,y of the sprite
var selected_name = ["s_keybindings_button","s_back_button"]
var num_buttons = 2
var sprite_offset = Vector2(-112,64)
func _ready():
move_selector(0)
set_process_input(true)
func move_selector(v):
if str(v) == "down":
if selected >= (num_buttons - 1):
selected = 0
else:
selected += 1
elif str(v) == "up":
if selected <= 0:
selected = (num_buttons - 1)
else:
selected -= 1
else:
selected = v
# move sprite
set_pos(Vector2( get_node("../" + selected_name[selected]).get_pos().x + sprite_offset.x , get_node("../" + selected_name[selected]).get_pos().y + sprite_offset.y))
func option_accept():
get_node("../" + selected_name[selected]).accept()
func _input(event):
# keyboard
if event.is_action_pressed("ui_down"):
move_selector("down")
elif event.is_action_pressed("ui_up"):
move_selector("up")
elif event.is_action_pressed("ui_accept"):
option_accept()
Menu switching is from a function in /menu_cont:
var cur_menu
var n_main_cont
var n_settings_cont
var n_bindings_cont
func _ready():
n_main_cont = get_node("main_cont")
n_settings_cont = get_node("settings_cont")
n_bindings_cont = get_node("bindings_cont")
cur_menu = n_main_cont
# disable everything but main menu
remove_child(n_settings_cont)
remove_child(n_bindings_cont)
# number of buttons in each menu
#var num_buttons = {n_main_cont = Vector2(1,4), n_settings_cont = Vector2(1,1), n_bindings_cont = Vector2(1,1)}
# change current menu, argument is a node
func change_menu(m):
remove_child(cur_menu)
print("change_menu(" + m + ")")
if m == "main":
add_child(n_main_cont)
cur_menu = n_main_cont
elif m == "settings":
add_child(n_settings_cont)
cur_menu = n_settings_cont
elif m == "bindings":
add_child(n_bindings_cont)
cur_menu = n_bindings_cont
When I run the scene, the main menu behaves as expected, then when swapping to the settings menu the mouseover behavior doesn't work and the debugger gives me a 'method not found' message every time the mouse enters a button's area. All of the other navigation works: using keys to move and accept options as well as clicking on them all go swimmingly.
The project is at https://github.com/Ach-Fusco/Test_Project
Thanks for any help!