- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 4.2k
 
          Refactor EntityEvent to support ContainsEntity, unlocking the use of kinded entities with observers
          #21408
        
          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
base: main
Are you sure you want to change the base?
Conversation
| 
           It looks like your PR is a breaking change, but you didn't provide a migration guide. Please review the instructions for writing migration guides, then expand or revise the content in the migration guides directory to reflect your changes.  | 
    
          
 I'm reluctant to do this as it will break object safety. That will be a pretty widespread problem when trying to fix this though.  | 
    
          
 It won't be needed with @cart 's suggestion. I'll revise the CL. How do we feel about immutable entity events though?  | 
    
          
 Generally open to it, but haven't thought about this hard enough to be convinced that they're the only solution :)  | 
    
This reverts commit 1cee14f.
| 
           I've updated the PR with the simplified implementation. Great idea! :) 
 I'm open to suggestions! From my usage of the engine so far, I have yet to find a use case for propagating events, which as I understand, is the only reason entity events are mutable. So from my perspective, I could easily envision all entity events being immutable by default, and only mutable if  If we do that, we could then implement   | 
    
| 
           On the topic of immutable entity events, I'm on board for that, but I think we'd do that at the trait level and break it out as  
 Making them mutable would also theoretically enable a bulk API (ex:   | 
    
This change adds a new `SetEntityEventTarget` to handle mutable entity events.
| 
           I've added  For me, the only remaining issue is the  Should I split that into a separate PR or should I continue working in this PR? Any opinions on how to refactor trigger? In my opinion, the current implementation goes against Rust nomenclature. Typically in Rust we have functions like  To fix this properly, I think   | 
    
| 
           I looked into refactoring  This is because we have to guarantee the entity event matches the referenced entity. Instead, we need to use: And this seems to work just fine. So I've updated the tests to use that instead, which makes the PR ready for review! (PS: Sorry about lack of commit message in the last commit, I forgot to set it 😅 )  | 
    
EntityEvent to support ContainsEntityEntityEvent to support ContainsEntity, unlocking the use of kinded entities with observers
      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.
Very nice: that's a very minimal change set. Can you please add a migration guide? We've lost a method on EntityEvent.
| 
           While I agree with the general direction, I see a flaw here: 
 Lastly, considering the prior implications,   | 
    
          
 Yeah I find this bit concerning. I don't really want to be in the business of " From my perspective the "public facing entity contract" is   | 
    
          
 That's why we pulled out the mutating functions out of  
 If it were up to me, a lot of Bevy API would use   | 
    
This is related to #21384
Objective
The goal of this change is to enable
EntityEventto work with any Entity-like type which implementsContainsEntity.Solution
EntityEvent::event_targetfromEntityto&impl ContainsEntityevent_target().entity()instead ofevent_target()EntityEvent::event_target_mutintoEntityEvent::set_event_targetI'm not fully happy with this solution yet, but I'm opening the PR to discuss the options from here.
Mainly the issue revolves around
set_event_target. To make this work, we need the underlying type to be constructible from an entity. This may not always be safe from user's perspective (it wouldn't be withInstance<T>for example).Based on my understanding of the system, an event's target is only mutated in the case of event propagation, which makes sense.
To work around this, I propose that we flag
EntityEventsas immutable or not; or "not propagatable" or not (I'm open to other terminology! :P).This would allow us to implement
set_event_targetasunreachable()!and throw an error instead if the user tries to propagate such an event. We could even maybe enforce this compile time but it'll require additional complexity (mostly in form of different permutations oftrigger_*method family).Testing
test_derive_entity_eventto cover all permutations