Driving a Godot 4 game from a script with Viewport.push_input: touch events for gameplay, mouse events for buttons

I wanted proof that my mobile puzzle game worked as a running program, not just as passing unit tests: swipe a block, open the pause menu, win a level, and look at the pixels. No device was connected. The whole thing turned out to need one API call, Viewport.push_input, and two rules about which event type reaches which code.

TL;DR — get_tree().root.push_input(event, true) delivers a synthetic InputEvent in viewport coordinates (so your 1080×1920 design space, whatever the window size). Gameplay code that listens in _unhandled_input gets InputEventScreenTouch / InputEventScreenDrag; Button and other Controls answer to InputEventMouseButton. Take screenshots with get_viewport().get_texture().get_image().save_png().

The setup

A portrait puzzle game: 1080×1920 design resolution, stretch mode canvas_items with aspect keep, so on a desktop the window is 540×960 and everything is scaled by 0.5. The board is a Node2D; a swipe is detected in _unhandled_input from InputEventScreenTouch (press) followed by InputEventScreenDrag events, and the block slides in the dominant direction once the finger has travelled 36 design pixels. Menus, popups and the pause button are ordinary Control nodes. input_devices/pointing/emulate_touch_from_mouse is on so the game is playable with a mouse on desktop.

The automation is a Node added to the main scene when the game is launched with a user argument; it reads a list of steps such as red_1:up (swipe the block called red_1 upward) or popup:Resume (click the button labelled Resume in the topmost popup), and saves a PNG after each one.

What I expected

Feeding events through Input.parse_input_event() in window coordinates, and having to compute the window-to-viewport scale myself. I also expected a Button to react to a synthetic touch, since real touches press buttons on the phone.

What actually happens

Viewport.push_input(event, in_local_coords) exists precisely for this, and with the second argument true the position is taken in the viewport’s own coordinate space: I could compute a block’s centre from its grid cell in design pixels and never think about the window size. The events go through the normal pipeline: GUI first, then _unhandled_input.

Two things needed care:

  1. A synthetic InputEventScreenTouch does not press a Button. Real touches do, because the engine’s emulate_mouse_from_touch turns a physical touch into a mouse event before the GUI sees it; an event pushed by script is not translated. So buttons are clicked with InputEventMouseButton, pressed then released, at the button’s get_global_rect().get_center().
  2. With the pause popup open, swipes did nothing (correct: the game checks for an open popup) but one test sequence still restarted the level. The swipe started on the popup’s Restart button and its release landed inside the same button, so the emulated mouse events triggered it. A synthetic touch stream can still click a Control if it starts and ends on it.

Path of a synthetic event pushed with push_input: GUI controls consume mouse button events, screen touch and drag events fall through to _unhandled_input where the swipe detector lives, and an open popup blocks the gameplay handler.

The fix

The whole driver fits in a few functions. _swipe emits one press, five drags of 20 px each, and a release; _tap emits a mouse press and release; _shot saves the viewport.

extends Node  # added by the main scene when "--capture" is in OS.get_cmdline_user_args()

func _swipe(from: Vector2, dir: Vector2) -> void:
    var root := get_tree().root
    var down := InputEventScreenTouch.new()
    down.index = 0
    down.pressed = true
    down.position = from
    root.push_input(down, true)
    for i in range(1, 6):
        var drag := InputEventScreenDrag.new()
        drag.index = 0
        drag.position = from + dir * 20.0 * i
        drag.relative = dir * 20.0
        root.push_input(drag, true)
    var up := InputEventScreenTouch.new()
    up.index = 0
    up.pressed = false
    up.position = from + dir * 100.0
    root.push_input(up, true)

func _tap(pos: Vector2) -> void:
    for pressed in [true, false]:
        var ev := InputEventMouseButton.new()
        ev.button_index = MOUSE_BUTTON_LEFT
        ev.pressed = pressed
        ev.position = pos
        ev.global_position = pos
        get_tree().root.push_input(ev, true)

func _shot(path: String) -> void:
    get_viewport().get_texture().get_image().save_png(path)

Between steps the driver waits on get_tree().create_timer(seconds).timeout, never on frame counts, because the run uses --disable-vsync and the frame rate is unbounded (the reason is in an earlier post on XWayland and vsync). A full scenario, swipes plus pause menu plus level-end popup plus three menu screens, runs in about 12 seconds and leaves a dozen PNGs to inspect.

godot --path . --disable-vsync -- --capture --level=51 \
  --steps=red_1:up,tap:HUD/PauseButton,popup:Resume,screen:Stats --out=/tmp/captures

Why it works

push_input hands the event to the viewport’s input pipeline as if it came from the window, minus the window-to-viewport transform when in_local_coords is true. The GUI pass looks up the Control under a mouse event and lets it consume the event; screen touch and drag events are not something BaseButton handles, so they continue to _unhandled_input on every node that implements it. Mouse emulation from touch happens at the display-server boundary, which a pushed event never crosses. Reading the pixels works because the root viewport keeps a render target even when nothing is shown in an editor; get_texture().get_image() copies it back from the GPU.

What I did not test

Multi-touch (a second index), InputEventScreenTouch on a Button with emulate_mouse_from_touch turned off, and Godot 4.3 and earlier for the push_input signature. The pixel checks were visual: I opened the PNGs, I did not compare them to reference images.

Facts

context: Godot 4.7.1 game with swipe detection in _unhandled_input and Control-based menus, automated end-to-end checks without a device
problem: how to inject swipes and button clicks from a script in design coordinates and capture the result
solution: get_tree().root.push_input(event, true) with ScreenTouch/ScreenDrag for gameplay and MouseButton for Controls; get_viewport().get_texture().get_image().save_png() for screenshots; real-time waits under --disable-vsync
verified_on: 2026-09-06
applies_to: [Godot 4.7.1, canvas_items stretch mode, desktop Linux runs]
does_not_apply_to: [headless runs (no render target), multi-touch scenarios]

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Retour en haut