Programmatically click a button

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

How do I use gdscript to programmatically click a button?

:bust_in_silhouette: Reply From: Gabriel

Have you tried emitting the “pressed” signal in the code?
I tried this code:

func _ready():
    $Button.emit_signal("pressed")

func _on_Button_pressed():
    print("pressed")

And it printed “pressed” even though I did not click the button.

yo thanks it worked

Titox | 2019-10-07 11:35

does not actually press button. Say in the case of a toggle button, it will not toggle it.

Allen Kennedy Jr. | 2020-06-24 23:21

Combine it with $Button.pressed = true. That works for me.

k3nzngtn | 2023-05-28 08:59

:bust_in_silhouette: Reply From: omggomb

Another way to do it, is to have a separate method for the functionality and then call that from the method that receives the regular pressed signal as well as from anywhere else you need. Unless you mean triggering the pressed visual effect of the button, then I have no idea.

If you want to make a button look like it’s pressed, just put it’s Pressed style in the Normal style, though if you’re just looking for the visual effect (like an evil AI is taking over your menu) than you can just replace the button with a Panel that has it’s styles changed when said pressing needs to happen.

Geko | 2022-10-07 16:50