I have been using a Prefab system like this for a while now, and wanted to ask for some comments. I am not sure if I am handling the SystemState correctly or if i am doing stuff manually that actually bevy can do itself already.
/// Trait that marks a struct as a Prefab
pub trait Prefab {
type Param: SystemParam;
fn spawn(self, commands: EntityCommands, param: SystemParamItem<Self::Param>);
}
/// Command that can be inserted to Commands
struct InsertPrefab<T: Prefab> {
prefab: T,
entity: Entity,
}
impl<T: Prefab + Send + Sync + 'static> Command for InsertPrefab<T> {
fn write(self, world: &mut World) {
let mut state: SystemState<(Commands, T::Param)> = SystemState::new(world);
let (mut commands, param) = state.get_mut(world);
let e_commands = commands.entity(self.entity);
self.prefab.spawn(e_commands, param);
state.apply(world);
}
}