{"record":{"id":"b8a2a4e53c221ce3","repo":"stride3d/stride","slug":"invalid-context-for-current-game","errorCode":null,"errorMessage":"Invalid context for current game.","messagePattern":"Invalid context for current game\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Games/GameWindow.cs","lineNumber":386,"sourceCode":"        internal void OnResume()\n        {\n            OnActivated(this, EventArgs.Empty);\n        }\n    }\n\n    public abstract class GameWindow<TK> : GameWindow\n    {\n        protected internal sealed override void Initialize(GameContext gameContext)\n        {\n            var context = gameContext as GameContext<TK>;\n            if (context != null)\n            {\n                GameContext = context;\n                Initialize(context);\n            }\n            else\n            {\n                throw new InvalidOperationException(\"Invalid context for current game.\");\n            }\n        }\n\n        internal GameContext<TK> GameContext;\n\n        protected abstract void Initialize(GameContext<TK> context);\n    }\n}\n","sourceCodeStart":368,"sourceCodeEnd":395,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Games/GameWindow.cs#L368-L395","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Let the platform-specific entry point (StrideApplication, StrideAndroidActivity, GameWindow host) create and pass its own GameContext instead of constructing one manually.","Check the Game subclass's generic type parameter and ensure the context passed is GameContext<TK> for that exact TK.","Update any custom bootstrap/main code after a Stride upgrade to use the current GameContext factory APIs."],"exampleFix":"// before\nvar game = new MyGame();\ngame.Initialize(new GameContext()); // wrong context type\n// after\nvar game = new MyGame();\ngame.Initialize(new GameContextSDL()); // match the platform's GameContext<TK>","handlingStrategy":"validation","validationCode":"if (gameContext is not GameContext<TK> ctx)\n    throw new ArgumentException(\"Context type does not match the game's platform\");\ngame.Initialize(ctx);","typeGuard":"bool IsValidContext<TK>(GameContext c) => c is GameContext<TK>;","tryCatchPattern":"try { game.Initialize(context); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Invalid context\"))\n{\n    logger.LogError(ex, \"GameContext type mismatch\");\n    throw;\n}","preventionTips":["Use the platform starter to create the GameContext instead of constructing it manually.","Keep the Game subclass's generic TK aligned with the platform you build for.","Re-check bootstrap code after Stride upgrades."],"tags":["game-engine","initialization","invalid-state"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}