-
-
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
Reactive ECS-integrated signals #16309
Conversation
/// System to automatically clean up unused [`Signal`]s. | ||
pub fn signal_cleanup(signals: Query<(Entity, &SignalRefcount)>, mut commands: Commands) { | ||
for (signal, refcount) in &signals { | ||
if Arc::strong_count(&refcount.0) == 1 { |
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 recommend doing what Asset handles do and have an Arc<T>
where T queues an event to despawn the entity when the Arc data is dropped. SignalRefcount
would probably want to hold a Weak<T>
then. This means you don't have to iterate through all signals, just ones that actually need to be cleaned up.
Edit: Unless you actually need to check the reference count for other reasons, you probably don't need SignalRefcount
at all with the above.
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.
Unless you actually need to check the reference count for other reasons, you probably don't need SignalRefcount at all with the above.
Not sure I get how, but I would love to get rid of it. Currently it's used for two things:
- If an entity (X) has a component subscribed to a signal, that's handled by spawning a new observer entity. Said observer entity gets a SignalRefcount component to ensure that the signal entity is not despawned while the observer entity is alive, i.e. while entity X has a subscription to the signal.
- If you have a Signal type (which is not a bevy component or resource), it internally holds a SignalRefcount so that the signal entity is not despawned while Signal is alive. You can pass this Signal into observer callbacks for button clicks or event handling systems or whatever in order to call signal.set(v) to update the signal's value.
I would love to get rid of the refcount so that Signal can be Copy again, but then I don't know how to automatically decide when to cleanup the signal entity.
Objective
C
that automatically updates based on the value of some other piece of data of typeS
Solution
S
SubscribedComponentSetup
component that given a mapping function from&S -> C
computes an initial valuec
based on the current value of the signal, insertsc
on the subscribed entity (the one with the setup component), and sets up an observer for monitoring changes to the signal data, and then calling the mapping function again to produce a newc
and updating the subscribed entity'sC
.Problems
TODO