stride3d/stride · error · ArgumentException

The offset must be either -1, 0 or 1

Error message

The offset must be either -1, 0 or 1

What it means

MoveImage in the sprite sheet editor moves selected sprites by exactly one slot per call; it throws ArgumentException when the offset is outside the -1..1 range. The offset shifts each selected sprite's index, and larger jumps were intentionally not implemented.

Solutions

  1. Pass only -1, 0 or 1; loop the call N times to move N positions.
  2. Clamp the offset before calling: Math.Max(-1, Math.Min(1, offset)).
  3. Change the caller to issue one-step moves per iteration.
  4. If larger jumps are needed, extend MoveImage to compute an absolute target index instead of a limited delta.

Example fix

// before
spriteSheetEditorViewModel.MoveImage(3);
// after
for (int i = 0; i < 3; i++)
    spriteSheetEditorViewModel.MoveImage(1);
Defensive patterns

Strategy: validation

Validate before calling

if (offset < -1 || offset > 1) throw new ArgumentOutOfRangeException(nameof(offset), offset, "Offset must be -1, 0 or 1");
spriteSheetEditorViewModel.MoveImage(offset);

Type guard

bool IsSingleStepOffset(int offset) => offset is >= -1 and <= 1;

Try / catch

try { vm.MoveImage(offset); }
catch (ArgumentException ex) { // clamp and retry once
    vm.MoveImage(Math.Sign(offset));
}

Prevention

When it happens

Trigger: Calling MoveImage(2), MoveImage(-2), or any offset with |offset| > 1, directly or via a UI command/binding that passes a computed step size larger than one.

Common situations: Binding keyboard shortcuts or toolbar commands that pass a multi-slot displacement, programmatically moving sprites several positions at once instead of iterating.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/87bde2596cdd2e3b. Report an issue: GitHub.

Appendix: source

Thrown at sources/editor/Stride.Assets.Presentation/AssetEditors/SpriteEditor/ViewModels/SpriteSheetEditorViewModel.cs:371

            SelectedSprites.Clear();
            SelectedSprites.AddRange(imagesToSelect.Select(FindViewModel));
        }

        private void SelectNextSprite(int offset)
        {
            if (SelectedSprites.Count == 1)
            {
                var index = Sprites.IndexOf(SelectedSprites.Cast<SpriteInfoViewModel>().First());
                index = (Sprites.Count + index + offset) % Sprites.Count;
                SelectedSprites.Clear();
                SelectedSprites.Add(Sprites[index]);
            }
        }

        private void MoveImage(int offset)
        {
            if (offset < -1 || offset > 1)
                throw new ArgumentException("The offset must be either -1, 0 or 1");

            var imagesToMove = new List<SpriteInfoViewModel>(SelectedSprites.Cast<SpriteInfoViewModel>());
            var toReselect = imagesToMove.Select(sivm => sivm.GetSpriteInfo()).ToList();
            foreach (var selectedImage in SelectedSprites.Cast<SpriteInfoViewModel>().OrderBy(x => x.Index * -offset))
            {
                if (selectedImage.Index + offset < 0 || selectedImage.Index + offset >= Sprites.Count)
                {
                    imagesToMove.Remove(selectedImage);
                    continue;
                }
                var targetImage = Sprites[selectedImage.Index + offset];
                if (SelectedSprites.Contains(targetImage) && !imagesToMove.Contains(targetImage))
                {
                    imagesToMove.Remove(selectedImage);
                }
            }
            if (imagesToMove.Count > 0)
            {

View on GitHub (pinned to 96fad776d2)