How to simulate a click on to a texture button?

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

I would like to simulate a mouse click on to a texture button via GDScript.
So I tried the following function:

func simulate_btn_click(main_btn):
    # main_btn.button is a TextureButton instance
    var event = InputEventMouseButton.new()
    event.position = main_btn.button.rect_global_position
    event.button_index = BUTTON_LEFT
    event.pressed = true
    get_tree().input_event(event)
    event.pressed = false
    get_tree().input_event(event)

In this code main_btn.button is a TextureButton instance.
Then calling that function like this:

call_deferred("simulate_btn_click", button)

while processing a mouse click on to another button does nothing.
The texture (toggle) button does not change its texture, it’s logic is not being processed.

Any ideas what I am missing here?

What does

simulate

mean?

Bimi124 | 2020-12-18 09:22

Well, the user itself does not really click on the texture button.
I want to simulate that click, as if the user would have clicked on to the button.

haimat | 2020-12-18 09:47

:bust_in_silhouette: Reply From: Bimi124

I solved this using a little trick. Her`s my code:

extends TextureButton

func simulateClick():
	var old_texture = texture_normal
	texture_normal = texture_pressed
	emit_signal("button_down")
	yield(get_tree().create_timer(0.2), "timeout")#to make sure the user sees the click event. Adapt the waiting time if needed.
	texture_normal = old_texture
	emit_signal("button_up")
	emit_signal("pressed")

Oh, by the way, I used emit_signal() to technically simulate clicking a button. Tis requires a new function for your button. Ther`s only one problem: If the program and the user click the button at one time the click event is emited two times…

Thanks, but I was hoping there is a way to really simulate the click it self.
Just as if the user would really have clicked on to the button …

haimat | 2020-12-18 10:24