FyroxEngine/Fyrox · error

Graphics context is uninitialized!

Error message

Graphics context is uninitialized!

What it means

as_initialized_ref() attempts to narrow a GraphicsContext enum to its Initialized variant and returns a reference to the initialized context. If the context is still Uninitialized (no window/renderer created yet), the method panics instead of returning a reference.

Solutions

  1. Only call as_initialized_ref after the context is confirmed Initialized
  2. Match on GraphicsContext::Initialized yourself instead of force-casting
  3. Defer renderer access to Plugin::update where the context exists

Example fix

// before
let ctx = engine.get_graphics_context().as_initialized_ref();
// after
if let GraphicsContext::Initialized(ctx) = engine.get_graphics_context() {
    // use ctx.renderer
}
Defensive patterns

Strategy: type-guard

Validate before calling

let ready = matches!(engine.get_graphics_context(), GraphicsContext::Initialized(_));

Type guard

fn initialized(ctx: &GraphicsContext) -> Option<&InitializedGraphicsContext> { if let GraphicsContext::Initialized(c) = ctx { Some(c) } else { None } }

Try / catch

std::panic::catch_unwind(|| ctx.as_initialized_ref()) // prefer if-let instead

Prevention

When it happens

Trigger: Calling engine.get_graphics_context().as_initialized_ref() before Engine::initialize created the graphics context, or after it was set to Uninitialized (e.g. context destroyed).

Common situations: Accessing the renderer in Plugin::init or first frame before context creation; headless/CI runs without a display where the context never initializes; code assuming the context is always initialized.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of FyroxEngine/Fyrox@76c91aad8e (2026-09-10). Data as JSON: /api/errors/cc50fce27188b8d5. Report an issue: GitHub.

Appendix: source

Thrown at fyrox-impl/src/engine/mod.rs:284

/// with which it was created and some of the main window parameters (position, size, etc.) and will re-use these
/// parameters on a next initialization attempt.
#[allow(clippy::large_enum_variant)]
pub enum GraphicsContext {
    /// Fully initialized graphics context. See [`InitializedGraphicsContext`] docs for more info.
    Initialized(InitializedGraphicsContext),

    /// Uninitialized (suspended) graphics context. See [`GraphicsContextParams`] docs for more info.
    Uninitialized(GraphicsContextParams),
}

impl GraphicsContext {
    /// Attempts to cast a graphics context to its initialized version. The method will panic if the context
    /// is not initialized.
    pub fn as_initialized_ref(&self) -> &InitializedGraphicsContext {
        if let GraphicsContext::Initialized(ctx) = self {
            ctx
        } else {
            panic!("Graphics context is uninitialized!")
        }
    }

    /// Attempts to cast a graphics context to its initialized version. The method will panic if the context
    /// is not initialized.
    pub fn as_initialized_mut(&mut self) -> &mut InitializedGraphicsContext {
        if let GraphicsContext::Initialized(ctx) = self {
            ctx
        } else {
            panic!("Graphics context is uninitialized!")
        }
    }
}

pub(crate) enum GameErrorSource {
    PluginMethod(&'static str),
    ScriptMethod {
        scene_handle: Handle<Scene>,

View on GitHub (pinned to 76c91aad8e)