B0003
由于命令是异步执行的,因此有可能在命令执行时,对不再存在的实体发出命令。
错误代码示例
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (use_0_and_despawn_1, use_1_and_despawn_0))
.run();
}
#[derive(Resource)]
struct MyEntities(Entity, Entity);
#[derive(Component)]
struct Hello;
fn setup(mut commands: Commands) {
let entity1 = commands.spawn_empty().id();
let entity2 = commands.spawn_empty().id();
commands.insert_resource(MyEntities(entity1, entity2));
}
fn use_0_and_despawn_1(mut commands: Commands, my_entities: Res<MyEntities>) {
commands.entity(my_entities.0).insert(Hello);
commands.entity(my_entities.1).despawn();
}
fn use_1_and_despawn_0(mut commands: Commands, my_entities: Res<MyEntities>) {
commands.entity(my_entities.1).insert(Hello);
commands.entity(my_entities.0).despawn();
}
这会导致恐慌,因为第一个执行的系统将取消分配第二个系统使用的实体。
默认的恐慌消息会告诉你哪个实体不存在(示例日志中 2v0
),哪个命令失败了(添加 Hello
组件),以及它来自哪个系统(use_1_and_despawn_0
)。
thread 'main' panicked at /bevy/crates/bevy_ecs/src/system/commands/mod.rs:1097:13:
error[B0003]: Could not insert a bundle (of type `use_entity_after_despawn::Hello`) for entity 2v0 because it doesn't exist in this World.
Encountered a panic when applying buffers for system `use_entity_after_despawn::use_1_and_despawn_0`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
要获取创建取消分配命令的系统,你可以为 bevy_ecs
板条箱启用 DEBUG 日志,例如通过设置环境变量 RUST_LOG=bevy_ecs=debug
或配置 LogPlugin
。这将记录
DEBUG system_commands{name="use_entity_after_despawn::use_0_and_despawn_1"}: bevy_ecs::world::entity_ref: Despawning entity 2v0
thread 'main' panicked at /bevy/crates/bevy_ecs/src/system/commands/mod.rs:1097:13:
error[B0003]: Could not insert a bundle (of type `use_entity_after_despawn::Hello`) for entity 2v0 because it doesn't exist in this World.
Encountered a panic when applying buffers for system `use_entity_after_despawn::use_1_and_despawn_0`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
从第一行,你可以知道实体 2v0
在从系统 use_0_and_despawn_1
执行命令时被取消分配了。在实际情况下,你可能会有很多日志行,你需要从恐慌消息中搜索确切的实体。