Switching focus between instantiated objects (Gamepad)

Godot Version

4.4.1

Question

I’m making a card game where the cards (TextureRect) are instantied into the player’s hand (HBoxContainer) as children. I’ve got mouse functionality working, now I’m trying to set up controller support. It can sense my gamepad just fine, but I’m not sure how to proceed. I’ve got this in my card script:

func _process(_delta: float) → void:
if gm.gamepad_active == true:
set_focus_mode(Control.FOCUS_ALL)
grab_focus()
if gm.gamepad_active == false:
set_focus_mode(Control.FOCUS_NONE)

The gamepad input successfully focuses on the last card in the hand’s hierarchy (not sure why it wouldn’t be the first). I don’t know how I would move between ui elements from there. I know you can set up neigbours in the focus tab of the inspector (though I’ve never done it, this is all new teritory for me), but I’m not sure how I’d do that on something that’s instantiated. Tested out find_next_valid_focus() to no luck. My first guess would be to make the focus more global and rely on the cards indexing?
Hope anyone can help, but thanks for reading regardless~

I tend to use grab_focus() through a deferred call:

node.call_deferred("grab_focus")

The deferred call means it happens after the process updates, which can prevent things from interfering with each other.

Control nodes also have properties like focus_neighbor_left and focus_neighbor_bottom that control how focus moves in response to controller stick movement, and focus_next which controls where focus goes when the player hits the tab key.