Skip to content

Commit

Permalink
feat: make World only ever require &self. (#270)
Browse files Browse the repository at this point in the history
This is wide but relatively shallow update that makes `World` only ever
require `&self`, allowing us to remove the system initialization stage
that was necessary to have `&mut World` access for initializing
resources and components.

The main change was to allow `Resources` to have resources added to it
with only a `&self` reference by using a `once_map` internally. This
meant that all resource cells now become `Option<SchemaBox>` on the
inside. This conceptually means that cells for reasources are lazily
initialized and always considered to be present, but they may or may not
actually contain a resource value.
  • Loading branch information
zicklag committed Dec 1, 2023
1 parent a44efcd commit 019a592
Show file tree
Hide file tree
Showing 20 changed files with 467 additions and 532 deletions.
6 changes: 1 addition & 5 deletions demos/features/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,7 @@ fn menu_system(
//
// This makes it easier to compose widgets that have differing access to the
// bones world.
//
// Note that all of the parameters of the system must have been initialized
// already, so if they are not initialized by another system, you have to use
// `world.init_param::<SomeParam>()` in the session plugin to initialize them.
world.run_initialized_system(demo_widget, ui);
world.run_system(demo_widget, ui);

ui.add_space(30.0);
});
Expand Down
6 changes: 3 additions & 3 deletions framework_crates/bones_bevy_renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ fn load_egui_textures(
// TODO: Avoid doing this every frame when there have been no assets loaded.
// We should should be able to use the asset load progress event listener to detect newly
// loaded assets that will need to be handled.
let mut bones_egui_textures = bones_egui_textures_cell.borrow_mut();
let mut bones_egui_textures = bones_egui_textures_cell.borrow_mut().unwrap();
// Take all loaded image assets and conver them to external images that reference bevy handles
bones_image_ids.load_bones_images(
asset_server,
Expand Down Expand Up @@ -665,7 +665,7 @@ fn egui_input_hook(
mut data: ResMut<BonesData>,
) {
if let Some(hook) = data.game.shared_resource_cell::<bones::EguiInputHook>() {
let hook = hook.borrow();
let hook = hook.borrow().unwrap();
let mut egui_input = egui_query.get_single_mut().unwrap();
(hook.0)(&mut data.game, &mut egui_input);
}
Expand Down Expand Up @@ -782,7 +782,7 @@ fn step_bones_game(world: &mut World) {
data.game.shared_resource_cell().unwrap()
}
};
let bones_window = bones_window.borrow_mut();
let bones_window = bones_window.borrow_mut().unwrap();

let is_fullscreen = matches!(&window.mode, WindowMode::Fullscreen);
if is_fullscreen != bones_window.fullscreen {
Expand Down
3 changes: 0 additions & 3 deletions framework_crates/bones_ecs/examples/pos_vel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ fn main() {
.add_system_to_stage(CoreStage::Update, pos_vel_system)
.add_system_to_stage(CoreStage::PostUpdate, print_system);

// Initialize our systems ( must be called once before calling stages.run() )
stages.initialize_systems(&mut world);

// Run our game loop for 10 frames
for _ in 0..10 {
stages.run(&mut world);
Expand Down
2 changes: 1 addition & 1 deletion framework_crates/bones_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ mod test {

#[test]
fn insert_comp_with_gap() {
let mut w = World::new();
let w = World::new();

#[derive(HasSchema, Default, Clone)]
#[repr(C)]
Expand Down
Loading

0 comments on commit 019a592

Please sign in to comment.