babalae/better-genshin-impact · error · Exception

切换区域[{areaName}]失败

Error message

切换区域[{areaName}]失败

What it means

Thrown by SwitchArea after TrySwitchArea returns false. TrySwitchArea opens the region dropdown, OCRs candidate texts, and matches against the localized area name. If no match is found within SwitchAreaCandidateTimeoutMs, it returns false and SwitchArea throws this generic Exception.

Source

Thrown at BetterGenshinImpact/GameTask/AutoTrackPath/TpTask.cs:2342

            {
                minCountry = forceCountry;
            }

            await SwitchArea(minCountry);
            return true;
        }

        return false;
    }

    internal async Task SwitchArea(string areaName)
    {
        if (await TrySwitchArea(areaName))
        {
            return;
        }

        throw new Exception($"切换区域[{areaName}]失败");
    }

    private async Task<bool> TrySwitchArea(string areaName)
    {
        GameCaptureRegion.GameRegionClick((rect, scale) => (rect.Width - 160 * scale, rect.Height - 60 * scale));
        var minCountryLocalized = this.stringLocalizer.WithCultureGet(this.cultureInfo, areaName);
        var candidatesText = "";
        var stopwatch = Stopwatch.StartNew();
        while (stopwatch.ElapsedMilliseconds < SwitchAreaCandidateTimeoutMs)
        {
            ct.ThrowIfCancellationRequested();
            using var ra = CaptureToRectArea();
            var list = FindSwitchAreaCandidates(ra);
            candidatesText = FormatSwitchAreaCandidateTexts(list);
            var matchRect = list
                .OrderByDescending(r => r.Y)
                .FirstOrDefault(r => IsSwitchAreaCandidateMatch(r.Text, minCountryLocalized, areaName));
            if (matchRect != null)

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Verify areaName is a valid region name for the current map (e.g. '蒙德', '璃月').
  2. Check the game language setting matches the expected localization.
  3. Increase SwitchAreaCandidateTimeoutMs if the UI is slow.
  4. Inspect the logged OCR candidate texts to diagnose OCR failures.
  5. Ensure the region dropdown is properly opened before the OCR scan.
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    await task.SwitchArea(areaName);
}
catch (Exception ex) when (ex.Message.StartsWith("切换区域"))
{
    Logger.LogWarning($"Area switch failed for {areaName}. Check OCR logs for candidate texts.");
    throw;
}

Prevention

When it happens

Trigger: Called from SwitchArea(areaName). TrySwitchArea's while-loop expires (SwitchAreaCandidateTimeoutMs) without finding a candidate whose text matches the localized area name via IsSwitchAreaCandidateMatch.

Common situations: The area name is misspelled or not a valid region in the current map; OCR fails to read the dropdown text due to font/rendering differences; the region dropdown didn't open or is in a different state; game language doesn't match the expected localized names; UI scaling distorts the OCR region.

Related errors


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