Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions crates/bevy_mod_scripting_core/src/bindings/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,6 @@ impl<'w> WorldAccessGuard<'w> {
}

/// Safely accesses the component by claiming and releasing access to it.
///
/// # Panics
/// - if the component does not exist
pub fn with_component<F, T, O>(&self, entity: Entity, f: F) -> Result<O, InteropError>
where
T: Component,
Expand All @@ -305,9 +302,6 @@ impl<'w> WorldAccessGuard<'w> {
}

/// Safely accesses the component by claiming and releasing access to it.
///
/// # Panics
/// - if the component does not exist
pub fn with_component_mut<F, T, O>(&self, entity: Entity, f: F) -> Result<O, InteropError>
where
T: Component,
Expand All @@ -327,6 +321,27 @@ impl<'w> WorldAccessGuard<'w> {
)
}

/// Safey modify or insert a component by claiming and releasing global access.
pub fn with_or_insert_component_mut<F, T, O>(&self,
entity: Entity,
f: F,
) -> Result<O, InteropError>
where
T: Component + Default,
F: FnOnce(&mut T) -> O,
{
self.with_global_access(|world| match world.get_mut::<T>(entity) {
Some(mut component) => f(&mut component),
None => {
let mut component = T::default();
let mut commands = world.commands();
let result = f(&mut component);
commands.entity(entity).insert(component);
result
}
})
}

/// Try to lookup a function with the given name on the given type id's namespaces.
///
/// Returns the function if found, otherwise returns the name of the function that was not found.
Expand Down
Loading