From 94462123d84a4888eab8762c80e3c9cf1242215c Mon Sep 17 00:00:00 2001 From: makspll Date: Sat, 5 Apr 2025 11:48:34 +0100 Subject: [PATCH 1/3] feat: improve errors when entity is unavailable --- assets/tests/insert_children/invalid_entity_errors.lua | 2 +- assets/tests/insert_children/invalid_entity_errors.rhai | 2 +- crates/bevy_mod_scripting_core/src/bindings/world.rs | 2 +- crates/lad_backends/mdbook_lad_preprocessor/src/sections.rs | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/assets/tests/insert_children/invalid_entity_errors.lua b/assets/tests/insert_children/invalid_entity_errors.lua index 2c5d51a2f4..7add8ed453 100644 --- a/assets/tests/insert_children/invalid_entity_errors.lua +++ b/assets/tests/insert_children/invalid_entity_errors.lua @@ -1,4 +1,4 @@ -local fake_entity = Entity.from_raw(9999) +local fake_entity = Entity.from_raw(0) assert_throws(function() world.insert_children(fake_entity, 1, {fake_entity}) diff --git a/assets/tests/insert_children/invalid_entity_errors.rhai b/assets/tests/insert_children/invalid_entity_errors.rhai index 2b960661ec..a7820bbf2c 100644 --- a/assets/tests/insert_children/invalid_entity_errors.rhai +++ b/assets/tests/insert_children/invalid_entity_errors.rhai @@ -1,4 +1,4 @@ -let fake_entity = Entity.from_raw.call(9999); +let fake_entity = Entity.from_raw.call(0); assert_throws(||{ world.insert_children.call(fake_entity, 0, [fake_entity]); diff --git a/crates/bevy_mod_scripting_core/src/bindings/world.rs b/crates/bevy_mod_scripting_core/src/bindings/world.rs index 2b2c749d51..c88dc84f61 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/world.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/world.rs @@ -536,7 +536,7 @@ impl<'w> WorldAccessGuard<'w> { /// checks if a given entity exists and is valid pub fn is_valid_entity(&self, entity: Entity) -> Result { let cell = self.as_unsafe_world_cell()?; - Ok(cell.get_entity(entity).is_some()) + Ok(cell.get_entity(entity).is_some() && entity.index() != 0) } /// Tries to call a fitting overload of the function with the given name and in the type id's namespace based on the arguments provided. diff --git a/crates/lad_backends/mdbook_lad_preprocessor/src/sections.rs b/crates/lad_backends/mdbook_lad_preprocessor/src/sections.rs index 0aa26b0c7f..9e2052f34a 100644 --- a/crates/lad_backends/mdbook_lad_preprocessor/src/sections.rs +++ b/crates/lad_backends/mdbook_lad_preprocessor/src/sections.rs @@ -622,6 +622,7 @@ impl IntoMarkdown for SectionItem<'_> { "#.trim(), ); + builder.append("\n\n"); } // we don't escape this, this is already markdown From 0fd5088239fc5f4e40119ed08ddce6a3bba0e712 Mon Sep 17 00:00:00 2001 From: makspll Date: Sat, 5 Apr 2025 11:52:04 +0100 Subject: [PATCH 2/3] also improve the error text --- crates/bevy_mod_scripting_core/src/error.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/bevy_mod_scripting_core/src/error.rs b/crates/bevy_mod_scripting_core/src/error.rs index 3e2a104c57..8b2bd2a967 100644 --- a/crates/bevy_mod_scripting_core/src/error.rs +++ b/crates/bevy_mod_scripting_core/src/error.rs @@ -1169,7 +1169,13 @@ macro_rules! invalid_index { macro_rules! missing_entity { ($entity:expr) => { - format!("Missing or invalid entity: {}", $entity) + { + if ($entity.index() == 0) { + format!("Invalid entity: {}. Are you trying to use an entity in a callback in which it's unavailable?", $entity) + } else { + format!("Missing or invalid entity: {}", $entity) + } + } }; } From 0f67b079087cf97f7db56dab7363d47bfc8131eb Mon Sep 17 00:00:00 2001 From: makspll Date: Sat, 5 Apr 2025 11:53:54 +0100 Subject: [PATCH 3/3] fix tests --- assets/tests/insert_children/invalid_entity_errors.lua | 5 +++-- assets/tests/insert_children/invalid_entity_errors.rhai | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/assets/tests/insert_children/invalid_entity_errors.lua b/assets/tests/insert_children/invalid_entity_errors.lua index 7add8ed453..d1212ac091 100644 --- a/assets/tests/insert_children/invalid_entity_errors.lua +++ b/assets/tests/insert_children/invalid_entity_errors.lua @@ -1,10 +1,11 @@ local fake_entity = Entity.from_raw(0) +local fake_entity_valid = Entity.from_raw(9999) assert_throws(function() - world.insert_children(fake_entity, 1, {fake_entity}) + world.insert_children(fake_entity_valid, 1, {fake_entity_valid}) end, "Missing or invalid entity") local entity = world.spawn() assert_throws(function() world.insert_children(entity, 1, {fake_entity}) -end, "Missing or invalid entity") \ No newline at end of file +end, "Are you trying to use an entity in a callback in which it's unavailable?") \ No newline at end of file diff --git a/assets/tests/insert_children/invalid_entity_errors.rhai b/assets/tests/insert_children/invalid_entity_errors.rhai index a7820bbf2c..2e4b014739 100644 --- a/assets/tests/insert_children/invalid_entity_errors.rhai +++ b/assets/tests/insert_children/invalid_entity_errors.rhai @@ -1,10 +1,11 @@ let fake_entity = Entity.from_raw.call(0); +let fake_entity_valid = Entity.from_raw.call(9999); assert_throws(||{ - world.insert_children.call(fake_entity, 0, [fake_entity]); + world.insert_children.call(fake_entity_valid, 0, [fake_entity_valid]); }, "Missing or invalid entity"); let entity = world.spawn_.call(); assert_throws(||{ world.insert_children.call(entity, 0, [fake_entity]); -}, "Missing or invalid entity"); \ No newline at end of file +}, "Are you trying to use an entity in a callback in which it's unavailable?"); \ No newline at end of file