0 votes

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
    event.pressed = false

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?

Godot version 3.2.3 stable
in Engine by (30 points)

What does



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.

1 Answer

0 votes

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

extends TextureButton

func simulateClick():
    var old_texture = texture_normal
    texture_normal = texture_pressed
    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

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...

by (82 points)

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 ...

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.