bevy/crates
Daniel McNab af20cad830 Add error messages for the spooky insertions (#2581)
# Objective

Sometimes, the unwraps in `entity_mut` could fail here, if the entity was despawned *before* this command was applied.

The simplest case involves two command buffers:
```rust
use bevy::prelude::*;
fn b(mut commands1: Commands, mut commands2: Commands) {
    let id = commands2.spawn().insert_bundle(()).id();
    commands1.entity(id).despawn();
}
fn main() {
    App::build().add_system(b.system()).run();
}
```

However, a more complicated version arises in the case of ambiguity:

```rust
use std::time::Duration;

use bevy::{app::ScheduleRunnerPlugin, prelude::*};
use rand::Rng;

fn cleanup(mut e: ResMut<Option<Entity>>) {
    *e = None;
}

fn sleep_randomly() {
    let mut rng = rand::thread_rng();
    std:🧵:sleep(Duration::from_millis(rng.gen_range(0..50)));
}

fn spawn(mut commands: Commands, mut e: ResMut<Option<Entity>>) {
    *e = Some(commands.spawn().insert_bundle(()).id());
}

fn despawn(mut commands: Commands, e: Res<Option<Entity>>) {
    let mut rng = rand::thread_rng();
    std:🧵:sleep(Duration::from_millis(rng.gen_range(0..50)));
    if let Some(e) = *e {
        commands.entity(e).despawn();
    }
}

fn main() {
    App::build()
        .add_system(cleanup.system().label("cleanup"))
        .add_system(sleep_randomly.system().label("before_despawn"))
        .add_system(despawn.system().after("cleanup").after("before_despawn"))
        .add_system(sleep_randomly.system().label("before_spawn"))
        .add_system(spawn.system().after("cleanup").after("before_spawn"))
        .insert_resource(None::<Entity>)
        .add_plugin(ScheduleRunnerPlugin::default())
        .run();
}
```

In the cases where this example crashes, it's because `despawn` was ordered before `spawn` in the topological ordering of systems (which determines when buffers are applied). However, `despawn` actually ran *after* `spawn`, because these systems are ambiguous, so the jiggles in the sleeping time triggered a case where this works.

## Solution

- Give a better error message
2021-09-01 20:59:25 +00:00
..
bevy_app Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_asset update ndk-glue to 0.4 (#2684) 2021-08-19 01:02:15 +00:00
bevy_audio Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_core Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_derive Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_diagnostic Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_dylib Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_dynamic_plugin Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_ecs Add error messages for the spooky insertions (#2581) 2021-09-01 20:59:25 +00:00
bevy_gilrs Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_gltf Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_input Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_internal update ndk-glue to 0.4 (#2684) 2021-08-19 01:02:15 +00:00
bevy_log Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_macro_utils Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_math Update glam requirement from 0.15.1 to 0.17.3 (#2500) 2021-08-19 01:32:31 +00:00
bevy_pbr Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_reflect Update glam requirement from 0.15.1 to 0.17.3 (#2500) 2021-08-19 01:32:31 +00:00
bevy_render Derive thiserror::Error for HexColorError (#2740) 2021-08-30 21:56:13 +00:00
bevy_scene System Param Lifetime Split (#2605) 2021-08-15 20:51:53 +00:00
bevy_sprite Document collide args (#2721) 2021-08-24 18:07:51 +00:00
bevy_tasks Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_text System Param Lifetime Split (#2605) 2021-08-15 20:51:53 +00:00
bevy_transform System Param Lifetime Split (#2605) 2021-08-15 20:51:53 +00:00
bevy_ui Use Explicit Names for Flex Direction (#2672) 2021-08-24 01:50:21 +00:00
bevy_utils derive Debug, Clone for FixedState (StableAHash) (#2694) 2021-08-24 01:31:39 +00:00
bevy_wgpu Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_window Not me ... us (#2654) 2021-08-15 20:08:52 +00:00
bevy_winit Not me ... us (#2654) 2021-08-15 20:08:52 +00:00