-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Trigger::entity()
only returns valid entities
#14558
base: main
Are you sure you want to change the base?
Conversation
This has itched me when reading the release docs. But isn't |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm nervous about all of our options here. Panicking is Very Bad, returning dummy entities is a silly footgun, and returning an option that always has to be unwrapped is frustrating.
In the spirit of #14275 / #12660, I'd prefer to store / return an Option
here over adding a panic, but I'm interested in @janhohenheim / @benfrankel / @james7132's opinions too.
Ping @MiniaczQ who I know has thoughts about this |
pub fn entity(&self) -> Entity { | ||
if self.trigger.entity == Entity::PLACEHOLDER { | ||
panic!( | ||
"called `Trigger::entity()` when the target entity was a `Entity::PLACEHOLDER`." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message does not help a user, as they will mostly not interact with Entity::PLACEHOLDER
and probably got this panic while trying to get the entity of a globally triggered observer. Add a hint to this like "Did you try [...]? There is no associated entity in that case."
This comment is also valid for the # Panics
documentation, which should mention under which circumstances an underlying Entity
would be a placeholder.
self.trigger.entity | ||
} | ||
|
||
/// Returns the entity that triggered the observer, if it's not a placeholder entity. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Returns the entity that triggered the observer, if it's not a placeholder entity. | |
/// Returns the entity that triggered the observer, if it's not [`Entity::PLACEHOLDER`]. |
.get_entity() | ||
.map(|entity| query.get(entity).is_ok()) | ||
.unwrap_or_default() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.get_entity() | |
.map(|entity| query.get(entity).is_ok()) | |
.unwrap_or_default() | |
.get_entity() | |
.is_some_and(|entity| query.get(entity).is_ok()) |
info!("Attack hit {}", name); | ||
if let Some(entity) = trigger.get_entity() { | ||
if let Ok(name) = name.get(entity) { | ||
info!("Attack hit {}", name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
info!("Attack hit {}", name); | |
info!("Attack hit {name}"); |
@alice-i-cecile my own preference in a vacuum would be to make The general consensus there is that we should not break pre-established naming conventions, so respecting that, the recommendation becomes to only add Note that the API as presented here is a bit ambiguous in terms of what triggered a |
Agreed with @janhohenheim, in that |
I wonder if we could always show |
My 2C here is that this is a silly problem that we have created for ourselves, and which we can solve by changing the problem. Why can't we have a A slightly less controversial option might be to make |
Yes that's what I proposed, I also tried prototyping it but the logic behind observers were kinda weird, so many different hashmaps of hashmaps where I was unsure if I need entity or option there.. so that's for someone else :> |
@SarthakSingh31 are you planning on completing this PR? |
Should be re-evaluated in the context of #15698. This could just be I will also say, the use-case for target-less observers seems small. |
I use "targetless" observers for state transition events. |
Objective
Fixes #14236
Solution
Made
Trigger::entity()
panic onEntity::PLACEHOLDER
and added aTrigger::get_entity()
Migration Guide
Anyone using
Trigger::entity()
should switch toTrigger::get_entity()
unless they know that the event will always be triggered on an entity.