From 6b8d743ae4cc2216a013f6bc39899a60ec9570b1 Mon Sep 17 00:00:00 2001 From: Max Whitehead <35712032+MaxCWhitehead@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:49:10 -0700 Subject: [PATCH] fix: Fix race condition on if EguiCtx is initialized before bones game (#378) Speculative fix for #377 (see for context) It seems both the `setup_egui` and `step_bones_game` systems are gated by if assets are loaded, which is an async process. My theory is that assets finish loading after PreUpdate, before Update, so bones steps without EguiCtx. This prevents `step_bones_game` from being run until EguiCtx is inserted in `setup_egui`. ~~Becaues `setup_egui` is gated by assets loaded, `step_bones_game` is also implicitly gated by assets loaded. I only check one `run_if` condition to avoid overhead executing systems that make up the core game loop.~~ --- framework_crates/bones_bevy_renderer/src/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/framework_crates/bones_bevy_renderer/src/lib.rs b/framework_crates/bones_bevy_renderer/src/lib.rs index e90f0246b9..5da5e698fe 100644 --- a/framework_crates/bones_bevy_renderer/src/lib.rs +++ b/framework_crates/bones_bevy_renderer/src/lib.rs @@ -28,7 +28,7 @@ use glam::*; use bevy_prototype_lyon::prelude as lyon; use bones_framework::prelude::{ - self as bones, BitSet, ComponentIterBitset, SchemaBox, SCHEMA_REGISTRY, + self as bones, BitSet, ComponentIterBitset, EguiCtx, SchemaBox, SCHEMA_REGISTRY, }; use prelude::convert::{IntoBevy, IntoBones}; use serde::{de::Visitor, Deserialize, Serialize}; @@ -309,6 +309,7 @@ impl BonesBevyRenderer { .init_resource::(); let assets_are_loaded = |data: Res| { + // Game is not required to have AssetServer, so default to true. data.asset_server .as_ref() .map(|x| x.load_progress.is_finished()) @@ -320,6 +321,8 @@ impl BonesBevyRenderer { .map(|x| !x.load_progress.is_finished()) .unwrap_or(true) }; + let egui_ctx_initialized = + |data: Res| data.game.shared_resource::().is_some(); // Add the world sync systems app.add_systems( @@ -350,7 +353,8 @@ impl BonesBevyRenderer { ), ) .chain() - .run_if(assets_are_loaded), + .run_if(assets_are_loaded) + .run_if(egui_ctx_initialized), ); if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {