babalae/better-genshin-impact · error · ArgumentException

鼠标坐标超出游戏窗口范围

Error message

鼠标坐标超出游戏窗口范围

What it means

Thrown by GlobalMethod.MoveMouseTo when either x or y coordinate is negative or exceeds the configured game width/height (_gameWidth, _gameHeight, defaulting to 1920x1080). The method validates against the game's logical pixel space before computing the scaled mouse position.

Source

Thrown at BetterGenshinImpact/Core/Script/Dependence/GlobalMethod.cs:187

    public static double[] GetGameMetrics()
    {
        return [_gameWidth, _gameHeight, _dpi];
    }

    public static void MoveMouseBy(int x, int y)
    {
        var realDpi = TaskContext.Instance().DpiScale;
        x = (int)(x * realDpi / _dpi);
        y = (int)(y * realDpi / _dpi);
        Simulation.SendInput.Mouse.MoveMouseBy(x, y);
    }

    public static void MoveMouseTo(int x, int y)
    {
        if (x < 0 || x > _gameWidth || y < 0 || y > _gameHeight)
        {
            throw new ArgumentException("鼠标坐标超出游戏窗口范围");
        }

        GameCaptureRegion.GameRegionMove((size, s2) =>
        {
            var scale = 1920.0 / _gameWidth;
            return (x * scale * s2, y * scale * s2);
        });
    }

    public static void Click(int x, int y)
    {
        MoveMouseTo(x, y);
        LeftButtonClick();
    }

    public static void LeftButtonClick()
    {
        Simulation.SendInput.Mouse.LeftButtonDown().Sleep(60).LeftButtonUp();

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Clamp coordinates to [0, width-1] × [0, height-1] before calling MoveMouseTo.
  2. Retrieve the current game metrics via GlobalMethod.GetGameMetrics() and use the returned width/height for bounds checking.
  3. Ensure SetGameMetrics has been called with the correct resolution matching the actual game capture.
  4. When computing coordinates from ratios, clamp: x = Math.Clamp((int)(ratio * width), 0, width - 1).

Example fix

// before
GlobalMethod.Click(targetX, targetY);

// after
var metrics = GlobalMethod.GetGameMetrics();
var w = (int)metrics[0];
var h = (int)metrics[1];
GlobalMethod.Click(
    Math.Clamp(targetX, 0, w),
    Math.Clamp(targetY, 0, h)
);
Defensive patterns

Strategy: validation

Validate before calling

// Validate mouse coordinates before calling MoveMouseTo/Click
var metrics = GlobalMethod.GetGameMetrics();
var gameW = (int)metrics[0];
var gameH = (int)metrics[1];
if (x < 0 || x > gameW || y < 0 || y > gameH)
    throw new ArgumentOutOfRangeException($"Coordinates ({x},{y}) out of bounds [0-{gameW}, 0-{gameH}]");
GlobalMethod.MoveMouseTo(x, y);

Prevention

When it happens

Trigger: Calling GlobalMethod.MoveMouseTo(x, y) or Click(x, y) with coordinates outside [0, _gameWidth] × [0, _gameHeight]. This includes negative values, values larger than the game resolution, or coordinates computed from an incorrect resolution assumption.

Common situations: Script hardcodes 1920x1080 coordinates but SetGameMetrics was called with a different resolution (e.g., 2560x1440). Off-by-one boundary (x = _gameWidth exactly triggers the error since the check is >). Coordinate computed from a percentage that rounds above the max.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/b98b7de93d291b56. Report an issue: GitHub.