How to visually press a button through script ?

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


I can’t figure how to visually press a button in GDScript. I want my button to appear pressed, I know how to send the signal that the button has been pressed.

I don’t want to use the “ui_accept” action and the button must still work with mouse clic.

Here is my code :

func _input(event):
    	if Input.is_action_just_pressed("up_action"):
		    $Node2D/Button.pressed = true

The console only print “False” except when I press the button with a mouse clic then trigger the “up_action”. The “up_action” contains the Up arrow key and the W key.

Do you know how can I visually press my button though GDScript ?

Thanks !

:bust_in_silhouette: Reply From: Inces

You have to enable toggle mode. This is the only way for button to have 2 states - pressed and unpressed. Otherwise pressed will be true only for duration of one frame.

If there is some reason You can’t have toggle buttons, there is another way around - You can access and change visual aspect of “button_pressed” in your Theme.

There’s no other way ? When I press the button with a mouse clic, the button remains pressed until I release the mouse button. Can I have the same result with a keyboard button without using toggle ?

NausiGally | 2022-03-10 16:56