{"record":{"id":"6dcfe9112dd51436","repo":"bevyengine/bevy","slug":"encountered-a-mismatched-world-this-systemstate-w","errorCode":null,"errorMessage":"Encountered a mismatched World. This SystemState was created from {this:?}, but a method was called using {other:?}.","messagePattern":"Encountered a mismatched World\\. This SystemState was created from (.+?), but a method was called using (.+?)\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/bevy_ecs/src/system/function_system.rs","lineNumber":423,"sourceCode":"        Param::apply(&mut self.param_state, &self.meta, world);\n    }\n\n    /// Returns `true` if `world_id` matches the [`World`] that was used to call [`SystemState::new`].\n    /// Otherwise, this returns false.\n    #[inline]\n    pub fn matches_world(&self, world_id: WorldId) -> bool {\n        self.world_id == world_id\n    }\n\n    /// Asserts that the [`SystemState`] matches the provided world.\n    #[inline]\n    #[track_caller]\n    fn validate_world(&self, world_id: WorldId) {\n        #[inline(never)]\n        #[track_caller]\n        #[cold]\n        fn panic_mismatched(this: WorldId, other: WorldId) -> ! {\n            panic!(\"Encountered a mismatched World. This SystemState was created from {this:?}, but a method was called using {other:?}.\");\n        }\n\n        if !self.matches_world(world_id) {\n            panic_mismatched(self.world_id, world_id);\n        }\n    }\n\n    /// Retrieve the [`SystemParam`] values.\n    ///\n    /// Returns an error if system parameter validation fails.\n    ///\n    /// # Safety\n    /// This call might access any of the input parameters in a way that violates Rust's mutability rules. Make sure the data\n    /// access is safe in the context of global [`World`] access. The passed-in [`World`] _must_ be the [`World`] the [`SystemState`] was\n    /// created with.\n    #[inline]\n    #[track_caller]\n    pub unsafe fn get_unchecked<'w, 's>(","sourceCodeStart":405,"sourceCodeEnd":441,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_ecs/src/system/function_system.rs#L405-L441","documentation":"SystemState caches a system's parameter state and is bound to the WorldId of the World it was built from. validate_world panics when a SystemState method is invoked against a World with a different id, because the cached state would be invalid for that world. This is bevy's guard against silently reusing per-world caches across worlds.","triggerScenarios":"Creating SystemState<Q> with one World and calling .get()/.get_mut() with another: static or cached state shared across two App/World instances, state extracted in a helper and reused after the world was rebuilt, or multi-world setups (editor preview world plus main world) sharing one state.","commonSituations":"Static/cached SystemState reused across tests that each create their own App; performance-motivated state caches that outlive their world; refactors that move world-creating code while keeping the cache.","solutions":["Store the WorldId next to the state and rebuild whenever SystemState::matches_world(world.id()) is false.","Do not cache SystemState in statics/globals across App instances; key it per world or construct it per call site.","In tests, create a fresh SystemState per test/world."],"exampleFix":"// before\nstatic mut STATE: Option<SystemState<Query<&Transform>>> = None; // reused across Worlds\n\n// after\nstruct Cached {\n    world_id: WorldId,\n    state: SystemState<Query<'static, 'static, &'static Transform>>,\n}\nfn cached(world: &mut World, slot: &mut Option<Cached>) -> &mut Cached {\n    let id = world.id();\n    if !slot.as_ref().is_some_and(|c| c.world_id == id) {\n        *slot = Some(Cached { world_id: id, state: SystemState::new(world) });\n    }\n    slot.as_mut().unwrap()\n}","handlingStrategy":"type-guard","validationCode":"if !state.matches_world(world.id()) {\n    *state = SystemState::new(world); // rebuild the cache for this world\n}\n// safe to call state.get_mut(world)","typeGuard":"fn state_matches_world<S: SystemParam>(state: &SystemState<S>, world: &World) -> bool {\n    state.matches_world(world.id())\n}","tryCatchPattern":null,"preventionTips":["Pair every cached SystemState with the WorldId it was built from.","Avoid static/global SystemState caches shared across App instances.","In helpers, take &mut World and derive the state from it instead of reusing cached state."],"tags":["bevy","ecs","system-state","world","cache-invalidation","panic"],"backgroundTag":"stale-cached-state","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T20:17:18.057Z"}