File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
crates/bevy_mod_scripting_core/src/bindings Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -327,6 +327,27 @@ impl<'w> WorldAccessGuard<'w> {
327
327
)
328
328
}
329
329
330
+ /// Safey modify or insert a component by claiming and releasing global access.
331
+ pub fn with_or_insert_component_mut < F , T , O > ( & self ,
332
+ entity : Entity ,
333
+ f : F ,
334
+ ) -> Result < O , InteropError >
335
+ where
336
+ T : Component + Default ,
337
+ F : FnOnce ( & mut T ) -> O ,
338
+ {
339
+ self . with_global_access ( |world| match world. get_mut :: < T > ( entity) {
340
+ Some ( mut component) => f ( & mut component) ,
341
+ None => {
342
+ let mut component = T :: default ( ) ;
343
+ let mut commands = world. commands ( ) ;
344
+ let result = f ( & mut component) ;
345
+ commands. entity ( entity) . insert ( component) ;
346
+ result
347
+ }
348
+ } )
349
+ }
350
+
330
351
/// Try to lookup a function with the given name on the given type id's namespaces.
331
352
///
332
353
/// Returns the function if found, otherwise returns the name of the function that was not found.
You can’t perform that action at this time.
0 commit comments