Skip to content

Commit

Permalink
fix: Fix race condition on if EguiCtx is initialized before bones game (
Browse files Browse the repository at this point in the history
#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.~~
  • Loading branch information
MaxCWhitehead committed Apr 5, 2024
1 parent cf03875 commit 6b8d743
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions framework_crates/bones_bevy_renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -309,6 +309,7 @@ impl BonesBevyRenderer {
.init_resource::<BonesGameEntity>();

let assets_are_loaded = |data: Res<BonesData>| {
// Game is not required to have AssetServer, so default to true.
data.asset_server
.as_ref()
.map(|x| x.load_progress.is_finished())
Expand All @@ -320,6 +321,8 @@ impl BonesBevyRenderer {
.map(|x| !x.load_progress.is_finished())
.unwrap_or(true)
};
let egui_ctx_initialized =
|data: Res<BonesData>| data.game.shared_resource::<EguiCtx>().is_some();

// Add the world sync systems
app.add_systems(
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6b8d743

Please sign in to comment.