Is there a way to emulate a user input such that it calls unhandled input?

Godot Version

4.2 stable

Question

I’m trying to write some unit tests in gdUnit and I’d like to emulate user input, which I handle on my character using unhandled input function. I’ve tried using Input but this I presume isn’t treated as an unhandled input under the hood. Is there a way to generate unhandled input without user input?

I think this is probably my bad layout - as I have Input.get_vector() under my unhandled input which strikes me as maybe not a correct approach. Open to suggestions

func controller_unplugged():
    var pause_event = InputEventAction.new()
    pause_event.action = "pause"
    pause_event.pressed = true
    Input.parse_input_event(pause_event)

I use these lines of code to emulate the “pause” action being pressed when the player disconnects a controller. The game checks whether the player presses the “pause” action in the _unhandled_input(event) function. I believe this should work for you as well.

First, create a new action event with InputEventAction.new() and assign it to a temporary variable; something like var emulated_action = InputEventAction.new(). Next, set this action’s action property to the corresponding action as String and the pressed property to true. Finally, parse the event by calling Input.parse_input_event(emulated_action).

Is this what you are looking for?

2 Likes

That looks right I’ll have to try it out but I presume it’ll solve it thank you!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.