stride3d/stride · error · InvalidOperationException

Invalid context for current game.

Error message

Invalid context for current game.

What it means

GameBase.Initialize throws this InvalidOperationException when the GameContext supplied to the game is not the concrete context type (GameContext<TK>) the platform-specific Game subclass expects. Stride routes context objects through Initialize and validates that the context matches the generic parameter of the game class; a mismatch means the game was constructed against one platform but initialized with another platform's context.

Solutions

  1. Let the platform-specific entry point (StrideApplication, StrideAndroidActivity, GameWindow host) create and pass its own GameContext instead of constructing one manually.
  2. Check the Game subclass's generic type parameter and ensure the context passed is GameContext<TK> for that exact TK.
  3. Update any custom bootstrap/main code after a Stride upgrade to use the current GameContext factory APIs.

Example fix

// before
var game = new MyGame();
game.Initialize(new GameContext()); // wrong context type
// after
var game = new MyGame();
game.Initialize(new GameContextSDL()); // match the platform's GameContext<TK>
Defensive patterns

Strategy: validation

Validate before calling

if (gameContext is not GameContext<TK> ctx)
    throw new ArgumentException("Context type does not match the game's platform");
game.Initialize(ctx);

Type guard

bool IsValidContext<TK>(GameContext c) => c is GameContext<TK>;

Try / catch

try { game.Initialize(context); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Invalid context"))
{
    logger.LogError(ex, "GameContext type mismatch");
    throw;
}

Prevention

When it happens

Trigger: Calling game.Initialize(context) (or running the game) with a GameContext created for a different platform/rendering backend than the Game<TK> subclass, or passing a base GameContext where the derived GameContext<TK> is required.

Common situations: Mixing platform assemblies (e.g. desktop GameContext with a custom cross-platform Game subclass whose generic parameter doesn't match), manually constructing GameContext instead of letting the platform starter create it, or upgrading Stride and keeping hand-rolled bootstrap code.

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/b8a2a4e53c221ce3. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Games/GameWindow.cs:386

        internal void OnResume()
        {
            OnActivated(this, EventArgs.Empty);
        }
    }

    public abstract class GameWindow<TK> : GameWindow
    {
        protected internal sealed override void Initialize(GameContext gameContext)
        {
            var context = gameContext as GameContext<TK>;
            if (context != null)
            {
                GameContext = context;
                Initialize(context);
            }
            else
            {
                throw new InvalidOperationException("Invalid context for current game.");
            }
        }

        internal GameContext<TK> GameContext;

        protected abstract void Initialize(GameContext<TK> context);
    }
}

View on GitHub (pinned to 96fad776d2)