bevyengine / bevyengine/bevy

`NameOrEntity` is difficult to `Query::get`

Open
#17,061 1 comment 0 reactions 0 assignees View on GitHub
A-ECS C-Feature D-Straightforward S-Ready-For-Implementation
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## What problem does this solve or what need does it fill?

Iterating a `Query` is pretty ergonomic: you just iterate through the query and it lets you display print the entity name. Quite nice! However if you need to use `Query::get` the situation is more complicated. If you `Query::::get`, you get back a `Result` since the entity may not exist - this is standard and expected, however handling this is a pain today.

For a concrete example, I have an observer that is triggered on `Pointer`. However another system can despawn this entity before the `Pointer` event is triggered. Trying to debug something, I added a `Query` and did `names.get(trigger.entity())`. `Result` doesn't implement `Display` though, so I unwrap the result - but since the entity is already despawned, the query doesn't find anything. Therefore, my app panics.

The "proper" way to handle this is with:

```rust
names.get(trigger.entity()).unwrap_or(NameOrEntityItem { name: None, entity: trigger.entity() })
```

## What solution would you like?

Ideally all this logic would be bundled into an associated function on `NameOrEntity`, for example `NameOrEntity::get_from_query(names, trigger.entity())`.

## What alternative(s) have you considered?

We could instead make an extension trait for `Query`, but this seems less discoverable and not that much more useful.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.