Today I learned
How do I test player input in Godot 4 headless, with real input actions?
Input.action_press() lets a headless test play the game through the same actions a player uses. But a press sent at the wrong moment of the frame is never seen by is_action_just_pressed(), and the test fails at random.
The fix: wait for process_frame (it fires right before every _process), press, hold for at least one frame, release.
func press(action: String, hold_s: float = 0.0) -> void:
await get_tree().process_frame # start of a frame
Input.action_press(action) # lands before _process
await get_tree().process_frame
if hold_s > 0.0:
await get_tree().create_timer(hold_s).timeout
Input.action_release(action)
await get_tree().process_frame
Run it from a user argument and quit with an exit code CI can read:
godot --headless --path game -- --test # OS.get_cmdline_user_args(), then get_tree().quit(0 or 1)
If you read input in _physics_process, await physics_frame instead.
Hit this while building a two-player co-op prototype: one script walks both players through a full scenario in about 80 seconds.
Docs: Input.action_press