Skip to content

Commit 1077d48

Browse files
authored
feat: improve errors when entity is unavailable (#410)
# Summary Many places in BMS use `Entity::from_raw(0)` as an invalid entity, because that index is not naturally assigned to any entity in Bevy. When an entity is not available in a callback, i.e. `on_script_loaded` people often try to interact with the entity global, this PR makes it so that using those entities, will throw a more meaningful error and point people to this fact. Also fixes minor newline issue in docs
1 parent bf42aa4 commit 1077d48

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
local fake_entity = Entity.from_raw(9999)
1+
local fake_entity = Entity.from_raw(0)
2+
local fake_entity_valid = Entity.from_raw(9999)
23

34
assert_throws(function()
4-
world.insert_children(fake_entity, 1, {fake_entity})
5+
world.insert_children(fake_entity_valid, 1, {fake_entity_valid})
56
end, "Missing or invalid entity")
67

78
local entity = world.spawn()
89
assert_throws(function()
910
world.insert_children(entity, 1, {fake_entity})
10-
end, "Missing or invalid entity")
11+
end, "Are you trying to use an entity in a callback in which it's unavailable?")
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
let fake_entity = Entity.from_raw.call(9999);
1+
let fake_entity = Entity.from_raw.call(0);
2+
let fake_entity_valid = Entity.from_raw.call(9999);
23

34
assert_throws(||{
4-
world.insert_children.call(fake_entity, 0, [fake_entity]);
5+
world.insert_children.call(fake_entity_valid, 0, [fake_entity_valid]);
56
}, "Missing or invalid entity");
67

78
let entity = world.spawn_.call();
89
assert_throws(||{
910
world.insert_children.call(entity, 0, [fake_entity]);
10-
}, "Missing or invalid entity");
11+
}, "Are you trying to use an entity in a callback in which it's unavailable?");

crates/bevy_mod_scripting_core/src/bindings/world.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ impl<'w> WorldAccessGuard<'w> {
536536
/// checks if a given entity exists and is valid
537537
pub fn is_valid_entity(&self, entity: Entity) -> Result<bool, InteropError> {
538538
let cell = self.as_unsafe_world_cell()?;
539-
Ok(cell.get_entity(entity).is_some())
539+
Ok(cell.get_entity(entity).is_some() && entity.index() != 0)
540540
}
541541

542542
/// 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.

crates/bevy_mod_scripting_core/src/error.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,13 @@ macro_rules! invalid_index {
11691169

11701170
macro_rules! missing_entity {
11711171
($entity:expr) => {
1172-
format!("Missing or invalid entity: {}", $entity)
1172+
{
1173+
if ($entity.index() == 0) {
1174+
format!("Invalid entity: {}. Are you trying to use an entity in a callback in which it's unavailable?", $entity)
1175+
} else {
1176+
format!("Missing or invalid entity: {}", $entity)
1177+
}
1178+
}
11731179
};
11741180
}
11751181

crates/lad_backends/mdbook_lad_preprocessor/src/sections.rs

+1
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ impl IntoMarkdown for SectionItem<'_> {
622622
</div>
623623
"#.trim(),
624624
);
625+
builder.append("\n\n");
625626
}
626627

627628
// we don't escape this, this is already markdown

0 commit comments

Comments
 (0)