Skip to content

feat: improve errors when entity is unavailable #410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions assets/tests/insert_children/invalid_entity_errors.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
local fake_entity = Entity.from_raw(9999)
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")
end, "Are you trying to use an entity in a callback in which it's unavailable?")
7 changes: 4 additions & 3 deletions assets/tests/insert_children/invalid_entity_errors.rhai
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
let fake_entity = Entity.from_raw.call(9999);
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");
}, "Are you trying to use an entity in a callback in which it's unavailable?");
2 changes: 1 addition & 1 deletion crates/bevy_mod_scripting_core/src/bindings/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, InteropError> {
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.
Expand Down
8 changes: 7 additions & 1 deletion crates/bevy_mod_scripting_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ impl IntoMarkdown for SectionItem<'_> {
</div>
"#.trim(),
);
builder.append("\n\n");
}

// we don't escape this, this is already markdown
Expand Down
Loading