stride3d/stride · error · InvalidOperationException

A scene matching the given

Error message

A scene matching the given {sceneId}, already exists.

What it means

SceneEditorGame.AddScene throws this InvalidOperationException when the scenes dictionary already contains the given sceneId. Scene ids are unique keys; adding a duplicate would corrupt the hierarchy.

Solutions

  1. Check whether the scene already exists before adding (skip if present).
  2. Remove the existing scene first if it should be replaced.
  3. Use a fresh Guid.NewGuid() per scene.
  4. Catch InvalidOperationException and treat the add as a no-op for duplicates.

Example fix

// before
sceneEditorGame.AddScene(sceneId, parentId); // may duplicate
// after
if (sceneEditorGame.GetScene(sceneId) == null)
    sceneEditorGame.AddScene(sceneId, parentId);
Defensive patterns

Strategy: validation

Validate before calling

if (game.GetScene(sceneId) != null) return; // already added

Try / catch

try { game.AddScene(sceneId, parentId); } catch (InvalidOperationException ex) when (ex.Message.Contains("already exists")) { /* idempotent: scene present */ }

Prevention

When it happens

Trigger: Calling AddScene twice with the same sceneId, e.g. re-running an import or re-executing a load routine without checking existence.

Common situations: Reloading the same asset twice; retry logic that re-invokes AddScene after a partial failure; id collision from a poor id generator.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


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

Appendix: source

Thrown at sources/editor/Stride.Assets.Presentation/AssetEditors/SceneEditor/Game/SceneEditorGame.cs:40

        public SceneEditorGame(TaskCompletionSource<bool> gameContentLoadedTaskSource, IEffectCompiler effectCompiler, string effectLogPath)
            : base(gameContentLoadedTaskSource, effectCompiler, effectLogPath)
        {

        }

        public event Action<Scene> SceneAdded;
        public event Action<Scene> SceneRemoved;

        /// <summary>
        /// Adds a new scene with the provided <paramref name="sceneId"/>.
        /// </summary>
        /// <param name="sceneId">The identifier of the scene to add.</param>
        /// <param name="parentId">The identifier of an existing parent scene, or <see cref="Guid.Empty"/>.</param>
        public void AddScene(Guid sceneId, Guid parentId)
        {
            if (sceneId == Guid.Empty) throw new InvalidOperationException($"{nameof(sceneId)} cannot be {nameof(Guid.Empty)}.");
            if (scenes.ContainsKey(sceneId)) throw new InvalidOperationException($"A scene matching the given {sceneId}, already exists.");

            EnsureContentScene();

            var scene = new Scene { Id = sceneId };
            var parent = parentId != Guid.Empty ? GetScene(parentId) : ContentScene;
            AddSceneToParent(scene, parent);
            scenes.Add(sceneId, scene);
            SceneAdded?.Invoke(scene);
        }

        /// <summary>
        /// Moves an existing scene identified by <paramref name="sceneId"/> under the parent scene identified by <paramref name="parentId"/>.
        /// </summary>
        /// <param name="sceneId">The identifier of the scene to move.</param>
        /// <param name="parentId">The identifier of an existing parent scene, or <see cref="Guid.Empty"/>.</param>
        public void MoveScene(Guid sceneId, Guid parentId)
        {
            if (sceneId == Guid.Empty)

View on GitHub (pinned to 96fad776d2)