How to properly "press" the button from code to make it use Focus Custom Styles?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Freeman

I have the button (control). The button uses the custom style, making it highlighted when pressed (the highlight is created by the Focus texture option of the Custom Styles in the Inspector of the button node).
It works when I physically press the button. It gets highlighted and highlighting lasts until another button is pressed.
But I wanted it to be/look already highlighted when the scene starts - before anything is pressed.
So far I tried to put in the func _ready():

  • $Control/Button.emit_signal("pressed")
  • $Control/Button.emit_signal("focus_entered")
  • $Control/Button.set_pressed(true)
  • $Control/Button.pressed = true

but at the start of the scene the button always stays in his default, non-highlighted appearance. However

func _on_button_pressed():
     print("Button pressed") 

seams to execute at the scene start, as there is “Button pressed” message in the output console - at least with $Control/Button.emit_signal("pressed") in func _ready():

Any idea how to be/make it look according to settings in Focus Custom Style at the very start of the scene?

:bust_in_silhouette: Reply From: njamster

Check out the documentation on Control-nodes. What you seek is:

func _ready():
    get_node("<PathToButton">.grab_focus()

Thank you njamster . grab_focus() is exactly what I needed.

Freeman | 2020-03-29 01:54