{"record":{"id":"cfdc2090e860c285","repo":"bevyengine/bevy","slug":"drawfunctions-must-be-added-to-the-world-as-a","errorCode":null,"errorMessage":"DrawFunctions<{}> must be added to the world as a resource before adding render commands to it","messagePattern":"DrawFunctions<(.+?)> must be added to the world as a resource before adding render commands to it","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/bevy_render/src/render_phase/draw.rs","lineNumber":372,"sourceCode":"        &mut self,\n    ) -> &mut Self\n    where\n        C::Param: ReadOnlySystemParam;\n}\n\nimpl AddRenderCommand for SubApp {\n    fn add_render_command<P: PhaseItem, C: RenderCommand<P> + Send + Sync + 'static>(\n        &mut self,\n    ) -> &mut Self\n    where\n        C::Param: ReadOnlySystemParam,\n    {\n        let draw_function = RenderCommandState::<P, C>::new(self.world_mut());\n        let draw_functions = self\n            .world()\n            .get_resource::<DrawFunctions<P>>()\n            .unwrap_or_else(|| {\n                panic!(\n                    \"DrawFunctions<{}> must be added to the world as a resource \\\n                     before adding render commands to it\",\n                    core::any::type_name::<P>(),\n                );\n            });\n        draw_functions.write().add_with::<C, _>(draw_function);\n        self\n    }\n}\n\nimpl AddRenderCommand for App {\n    fn add_render_command<P: PhaseItem, C: RenderCommand<P> + Send + Sync + 'static>(\n        &mut self,\n    ) -> &mut Self\n    where\n        C::Param: ReadOnlySystemParam,\n    {\n        SubApp::add_render_command::<P, C>(self.main_mut());","sourceCodeStart":354,"sourceCodeEnd":390,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_render/src/render_phase/draw.rs#L354-L390","documentation":"SubApp::add_render_command (draw.rs:372) panics when the DrawFunctions<P> resource has not been added to that world yet. Render commands for phase item type P are stored inside the DrawFunctions<P> resource, so the resource must be initialized before any command is added. The panic names P so you know which phase's resource is missing.","triggerScenarios":"Calling add_render_command on an app/world where init_resource::<DrawFunctions<P>>() has not run - most commonly calling it on the main app world instead of the Render sub-app, or a custom phase plugin that forgets to init the resource before registering commands.","commonSituations":"Custom render phases: forgetting render_app.init_resource::<DrawFunctions<MyPhaseItem>>() in the plugin; adding render commands in Plugin::build without going through app.get_sub_app_mut(RenderApp); using a built-in phase (e.g. Opaque3d) before the plugin that initializes its DrawFunctions is added; Bevy version migrations that moved where the resource is initialized.","solutions":["Init the resource first: render_app.init_resource::<DrawFunctions<P>>() in your plugin build, then call add_render_command.","Make sure you are operating on the render sub-app (app.get_sub_app_mut(RenderApp) / RenderApp), not the main world.","For built-in phases, ensure the corresponding core plugin (which inits DrawFunctions) is added before your plugin (add a plugin dependency)."],"exampleFix":"// before: added on the main app world, resource not present\napp.add_render_command::<Opaque3d, DrawCustom>();\n\n// after: go through the render sub-app and init the resource for custom phases\nif let Some(render_app) = app.get_sub_app_mut(RenderApp) {\n    render_app\n        .init_resource::<DrawFunctions<CustomPhaseItem>>() // for custom phases\n        .add_render_command::<CustomPhaseItem, DrawCustom>();\n}","handlingStrategy":"validation","validationCode":"if let Some(render_app) = app.get_sub_app_mut(RenderApp) {\n    if !render_app.world().contains_resource::<DrawFunctions<CustomPhaseItem>>() {\n        render_app.init_resource::<DrawFunctions<CustomPhaseItem>>();\n    }\n    render_app.add_render_command::<CustomPhaseItem, DrawCustom>();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always add render commands through the render sub-app (get_sub_app_mut(RenderApp)).","For custom phases, init_resource::<DrawFunctions<P>>() in the same plugin that registers commands.","For built-in phases, depend on the plugin that initializes their DrawFunctions."],"tags":["bevy","resource","render-app","panic","plugin-setup"],"backgroundTag":"missing-ecs-resource","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}