1
- use bevy_ecs:: component:: Component ;
2
- use bevy_ecs:: lifecycle:: HookContext ;
3
1
use bevy_ecs:: system:: { Commands , EntityCommands , IntoSystem , SystemId , SystemInput } ;
4
2
use bevy_ecs:: world:: { DeferredWorld , EntityWorldMut , World } ;
5
3
4
+ use crate :: owner:: OwnedBy ;
5
+
6
6
/// A callback defines how we want to be notified when a widget changes state. Unlike an event
7
7
/// or observer, callbacks are intended for "point-to-point" communication that cuts across the
8
8
/// hierarchy of entities. Callbacks can be created in advance of the entity they are attached
@@ -114,24 +114,6 @@ impl Notify for DeferredWorld<'_> {
114
114
}
115
115
}
116
116
117
- /// A component that hangs on to a registered one-shot system, and unregisters it when the
118
- /// component is despawned.
119
- #[ derive( Component ) ]
120
- #[ component( on_remove = on_despawn_callback_owner:: <I >, storage = "SparseSet" ) ]
121
- pub struct OwnedCallbackSystem < I : SystemInput + Send > ( SystemId < I , ( ) > ) ;
122
-
123
- fn on_despawn_callback_owner < I : SystemInput + Send + ' static > (
124
- mut world : DeferredWorld ,
125
- context : HookContext ,
126
- ) {
127
- let system_id = world
128
- . entity ( context. entity )
129
- . get :: < OwnedCallbackSystem < I > > ( )
130
- . unwrap ( )
131
- . 0 ;
132
- world. commands ( ) . unregister_system ( system_id) ;
133
- }
134
-
135
117
/// Methods for registering scoped callbacks.
136
118
pub trait RegisterOwnedCallback {
137
119
/// Registers a scoped one-shot system, with no input, that will be removed when the parent
@@ -161,7 +143,8 @@ impl RegisterOwnedCallback for EntityCommands<'_> {
161
143
let system_id = self . commands ( ) . register_system ( callback) ;
162
144
let owner = self . id ( ) ;
163
145
self . commands ( )
164
- . spawn ( ( OwnedCallbackSystem ( system_id) , crate :: owner:: OwnedBy ( owner) ) ) ;
146
+ . entity ( owner)
147
+ . add_one_related :: < OwnedBy > ( system_id. entity ( ) ) ;
165
148
Callback :: System ( system_id)
166
149
}
167
150
@@ -176,7 +159,8 @@ impl RegisterOwnedCallback for EntityCommands<'_> {
176
159
let owner = self . id ( ) ;
177
160
let system_id = self . commands ( ) . register_system ( callback) ;
178
161
self . commands ( )
179
- . spawn ( ( OwnedCallbackSystem ( system_id) , crate :: owner:: OwnedBy ( owner) ) ) ;
162
+ . entity ( owner)
163
+ . add_one_related :: < OwnedBy > ( system_id. entity ( ) ) ;
180
164
Callback :: System ( system_id)
181
165
}
182
166
}
@@ -189,7 +173,9 @@ impl RegisterOwnedCallback for EntityWorldMut<'_> {
189
173
let owner = self . id ( ) ;
190
174
let system_id = self . world_scope ( |world| world. register_system ( callback) ) ;
191
175
self . world_scope ( |world| {
192
- world. spawn ( ( OwnedCallbackSystem ( system_id) , crate :: owner:: OwnedBy ( owner) ) ) ;
176
+ world
177
+ . entity_mut ( owner)
178
+ . add_one_related :: < OwnedBy > ( system_id. entity ( ) ) ;
193
179
} ) ;
194
180
Callback :: System ( system_id)
195
181
}
@@ -205,7 +191,9 @@ impl RegisterOwnedCallback for EntityWorldMut<'_> {
205
191
let owner = self . id ( ) ;
206
192
let system_id = self . world_scope ( |world| world. register_system ( callback) ) ;
207
193
self . world_scope ( |world| {
208
- world. spawn ( ( OwnedCallbackSystem ( system_id) , crate :: owner:: OwnedBy ( owner) ) ) ;
194
+ world
195
+ . entity_mut ( owner)
196
+ . add_one_related :: < OwnedBy > ( system_id. entity ( ) ) ;
209
197
} ) ;
210
198
Callback :: System ( system_id)
211
199
}
0 commit comments