Godot Version
4.6
Question
Hello! I’m working on the UI in my game, and I’ve found that the code I used in 4.5 doesn’t work in another game in 4.6 for some reason, which I suspect may be because I’m trying to focus on a button that is currently invisible.
I have two pages of inventory, each holding four items which are represented as buttons. I prefer to set focus neighbors myself through code, since I have more buttons on screen at the same time, and Godot’s automatic focus detection may focus on buttons I don’t need focus on. Here is the code I use to set the focus neighbors for inventory buttons:
func row_sort(first_array, second_array):
for i in range(first_array.size()):
if i + 1 < first_array.size():
first_array[i].focus_neighbor_right = NodePath(first_array[i + 1].get_path())
else:
first_array[i].focus_neighbor_right = NodePath(first_array[0].get_path())
if i == 0:
first_array[i].focus_neighbor_left = NodePath(first_array[-1].get_path())
else:
first_array[i].focus_neighbor_left = NodePath(first_array[i - 1].get_path())
if i < second_array.size():
first_array[i].focus_neighbor_top = NodePath(second_array[i].get_path())
first_array[i].focus_neighbor_bottom = NodePath(second_array[i].get_path())
else:
first_array[i].focus_neighbor_top = NodePath(first_array[i].get_path())
first_array[i].focus_neighbor_bottom = NodePath(first_array[i].get_path())
I cal this function twice to set the neighbors for the first and the second row (of both pages), like this:
if i in [0, 1, 4, 5]:
first_row.append(new_item_button)
else:
second_row.append(new_item_button)
...
row_sort(first_row, second_row)
row_sort(second_row, first_row)
To flip the pages over, I simply connect the focus_entered signal to the flip pages functions, which simply turn one page visible, and the other invisible.
if i < 4:
page_1_grid.add_child(new_item_button)
new_item_button.focus_entered.connect(flip_page_to_1)
else:
page_2_grid.add_child(new_item_button)
new_item_button.focus_entered.connect(flip_page_to_2)
This all was working correctly in another game I’ve been developing in 4.5, however, in another game in 4.6 the focus just doesn’t go over to the next page, e.g. the right buttons just flip focus to the left buttons on the page, and the other way around. Is there any way to switch focus to invisible buttons in 4.6?