From 5bb4201f2e0afd3962648c05221c8d401d8c583c Mon Sep 17 00:00:00 2001 From: dataphract Date: Sun, 13 Feb 2022 00:14:37 +0000 Subject: [PATCH] add informative panic message when adding render commands to a DrawFunctions that does not exist (#3924) # Objective If a user attempts to `.add_render_command::()` on a world that does not contain `DrawFunctions

`, the engine panics with a generic `Option::unwrap` message: ``` thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /[redacted]/bevy/crates/bevy_render/src/render_phase/draw.rs:318:76 ``` ## Solution This PR adds a panic message describing the problem: ``` thread 'main' panicked at 'DrawFunctions must be added to the world as a resource before adding render commands to it', /[redacted]/bevy/crates/bevy_render/src/render_phase/draw.rs:322:17 ``` --- crates/bevy_render/src/render_phase/draw.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/bevy_render/src/render_phase/draw.rs b/crates/bevy_render/src/render_phase/draw.rs index b21e65e468..73628316f3 100644 --- a/crates/bevy_render/src/render_phase/draw.rs +++ b/crates/bevy_render/src/render_phase/draw.rs @@ -315,7 +315,16 @@ impl AddRenderCommand for App { ::Fetch: ReadOnlySystemParamFetch, { let draw_function = RenderCommandState::::new(&mut self.world); - let draw_functions = self.world.get_resource::>().unwrap(); + let draw_functions = self + .world + .get_resource::>() + .unwrap_or_else(|| { + panic!( + "DrawFunctions<{}> must be added to the world as a resource \ + before adding render commands to it", + std::any::type_name::

(), + ); + }); draw_functions.write().add_with::(draw_function); self }