babalae/better-genshin-impact · error · PartySetupFailedException

切换角色:无法为未指定槽位生成补位角色

Error message

切换角色:无法为未指定槽位生成补位角色

What it means

BuildDesiredTeamRoles fills any still-empty desired slot from the 'remaining' list (characters present but not pinned to a target). If remaining is empty while a desired slot is still null, there are more slots to fill than characters available — an inconsistent state between expectedTeamCount and recognized members.

Source

Thrown at BetterGenshinImpact/GameTask/Common/Job/SwitchCharacterStateMachineTask.cs:1074

            if (originalIndex < 0)
            {
                continue;
            }

            desired[index] = CreateTargetRole(index + 1, remaining[originalIndex].Name);
            remaining.RemoveAt(originalIndex);
        }

        for (var index = 0; index < desired.Length; index++)
        {
            if (desired[index] != null)
            {
                continue;
            }

            if (remaining.Count == 0)
            {
                throw new PartySetupFailedException("切换角色:无法为未指定槽位生成补位角色");
            }

            desired[index] = CreateTargetRole(index + 1, remaining[0].Name);
            remaining.RemoveAt(0);
        }

        return desired.Select(role => role!).ToList();
    }

    private static int? FindFirstMismatchSlot(
        IReadOnlyList<TargetRole> desiredRoles,
        IReadOnlyList<TeamSlotSnapshot> currentSlots)
    {
        return desiredRoles
            .OrderBy(role => role.Slot)
            .FirstOrDefault(role =>
                role.Slot > currentSlots.Count || !role.Matches(currentSlots[role.Slot - 1].Name))
            ?.Slot;

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Ensure _expectedTeamCount and _currentTeamSlots stay consistent — recompute both from a single recognition pass.
  2. Before BuildDesiredTeamRoles, filter out null-named slots or reconcile the count; do not build desired[] longer than available names.
  3. If the team genuinely has fewer members, shrink the desired array rather than over-filling.

Example fix

// before
if (remaining.Count == 0)
{
    throw new PartySetupFailedException("切换角色:无法为未指定槽位生成补位角色");
}

// after
if (remaining.Count == 0)
{
    desired = desired.Where(role => role != null).ToArray() as TargetRole?[];
    break;
}
Defensive patterns

Strategy: validation

Validate before calling

if (currentSlots.Count(slot => !string.IsNullOrWhiteSpace(slot.Name)) < requestedRoles.Count(r => r.Slot <= currentSlots.Count))
{
    throw new PartySetupFailedException("切换角色:可控角色数少于待填充槽位数,请重新识别队伍");
}

Try / catch

try { await switchTask.Start(...); }
catch (PartySetupFailedException ex) when (ex.Message.Contains("无法为未指定槽位生成补位角色"))
{ _logger.LogError(ex, "队伍槽位与识别到的角色数不一致"); }

Prevention

When it happens

Trigger: After placing requested roles and restoring original-slot characters, a desired[index] is null and remaining.Count==0; i.e. currentSlots.Count (the array length) exceeds the number of name-bearing characters left to distribute.

Common situations: expectedTeamCount/_currentTeamSlots length disagrees with the actual number of recognized avatars (some slots had null names that were already consumed by the first projection, or a role consumed a name that was also pinned); a rebuild path shrank the team mid-operation; co-op controllable-slot count changed after the snapshot.

Related errors


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