How to focus on an invisible button in Godot 4.6?

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?

How are you making the button invisible?

The parent of the button is a grid, and page 2 of the inventory (aka the grid) is invisible at the start. It’s supposed to become visible when a button in the grid comes in focus, but this never happens as the button doesn’t come in focus because (I think) it’s invisible.

When you say “invisible” do you mean visible = false? Or you modulated the Alpha to 0? How did you make it invisible?

1 Like

I made it invisible in the editor, which I think is the same thing as visible = false.

Right, so if it’s a Control (green node), or inherited from a Control when you set its visibility to false, it no longer exists on the screen. So what surprises me is it worked at all before.

So yes, you have to make it visible before you can focus on it.

1 Like

Alright, I fixed it! Thanks for giving me the idea to use modulate instead! Now it works as intended! :slight_smile:

1 Like