babalae/better-genshin-impact · error · InvalidOperationException

不在主界面,无法识别小地图坐标

Error message

不在主界面,无法识别小地图坐标

What it means

Thrown by GetPositionFromMapWithMatchingMethod when the game screen is not on the main overworld UI (GameUiCategory.Main). The method captures the game window and runs Bv.IsInMainUi, which image-matches for the minimap area and main-UI UI elements. Since minimap-based coordinate recognition only works when the minimap is visible, any other screen state (menu, dialogue, loading, big map, pause) is a hard failure.

Source

Thrown at BetterGenshinImpact/Core/Script/Dependence/Genshin.cs:267

    /// <summary>
    /// 获取当前在小地图上的位置坐标,如果缓存时间内有匹配成功的坐标优先返回缓存坐标,否则调用NavigationInstance的getPositionStable
    /// </summary>
    /// <param name="mapName">大地图名称</param>
    /// <param name="cacheTimeMs">缓存时间,单位毫秒,默认900ms</param>
    /// <returns>包含X和Y坐标的Point2f结构体</returns>
    public Point2f? GetPositionFromMap(string mapName, int cacheTimeMs = 900)
    {
        var matchingMethod = TaskContext.Instance().Config.PathingConditionConfig.MapMatchingMethod;
        return GetPositionFromMapWithMatchingMethod(mapName,matchingMethod, cacheTimeMs);
    }
    
    public Point2f? GetPositionFromMapWithMatchingMethod(string mapName, string matchingMethod, int cacheTimeMs = 900)
    {
        using var imageRegion = CaptureToRectArea();
        if (!Bv.IsInMainUi(imageRegion))
        {
            throw new InvalidOperationException("不在主界面,无法识别小地图坐标");
        }
        return MapManager.GetMap(mapName, matchingMethod)
            .ConvertImageCoordinatesToGenshinMapCoordinates(LazyNavigationInstance.Value
                .GetPositionStableByCache(imageRegion, mapName, matchingMethod, cacheTimeMs));
    }
    
    /// <summary>
    /// 获取当前在小地图上的位置坐标, 局部匹配, 需要世界坐标, 在坐标附近匹配, 失败不进行全局匹配
    /// </summary>
    /// <param name="mapName">大地图名称</param>
    /// <param name="x">世界坐标x</param>
    /// <param name="y">世界坐标y</param>
    /// <returns>包含X和Y坐标的Point2f结构体</returns>
    public Point2f? GetPositionFromMap(string mapName, float x, float y)
    {
        using var imageRegion = CaptureToRectArea();
        if (!Bv.IsInMainUi(imageRegion))
        {

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Call ReturnMainUi or press Esc to close any open menu before invoking GetPositionFromMap.
  2. Add a delay after teleport or loading-screen transitions to ensure the main UI has fully rendered.
  3. Call Bv.IsInMainUi (or Bv.WhichGameUi) yourself before GetPositionFromMap and skip or wait if false.
  4. Wrap the call in try/catch(InvalidOperationException) and implement a retry loop with a bounded delay.

Example fix

// before
genshin.GetPositionFromMap("Teyvat");

// after
if (Bv.IsInMainUi(TaskContext.Instance().CaptureToRectAreaUsingAtLeastOnce()))
{
    genshin.GetPositionFromMap("Teyvat");
}
else
{
    await new ReturnMainUiTask().Start(CancellationContext.Instance.Cts.Token);
    genshin.GetPositionFromMap("Teyvat");
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate game UI state before calling GetPositionFromMap
using var region = TaskContext.Instance().CaptureToRectAreaUsingAtLeastOnce();
if (!Bv.IsInMainUi(region))
{
    // return to main UI first
    await new ReturnMainUiTask().Start(CancellationContext.Instance.Cts.Token);
}

Try / catch

try
{
    var pos = genshin.GetPositionFromMap(mapName);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("不在主界面"))
{
    // game not on main UI — return to main UI and retry once
    await new ReturnMainUiTask().Start(CancellationContext.Instance.Cts.Token);
    await Task.Delay(2000);
    pos = genshin.GetPositionFromMap(mapName);
}

Prevention

When it happens

Trigger: Calling genshin.GetPositionFromMap(mapName) or GetPositionFromMapWithMatchingMethod(mapName, matchingMethod) while the game shows a non-overworld screen: Paimon menu (Esc), character screen, inventory, dialogue, loading screen, big map (M), or any domain/abyss interior that hides the standard minimap.

Common situations: Script calls GetPositionFromMap immediately after a teleport or scene transition before the loading screen finishes. Script left the game in a menu state (e.g. after SetTime without returning to main UI). Game window not focused or capture returning a stale/black frame.

Related errors


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