Back To

How to use prefabs in system?

I wanted to spawn enemies in a system using prefabs but I have no idea how to do this. In Amethyst examples prefabs are always used from World but I wanted to use them in a system, using Entities. When creating new entity from Entities I need to pass a second argument to with method, passing a Storage. Which storage is it when it comes to prefabs?
A simplified version of my code:

impl<'s> System<'s> for EnemySpawnerSystem {
    type SystemData = (
        WriteStorage<'s, Transform>,

    fn run(
        &mut self, 
            mut transforms,
        ): Self::SystemData
    ) {
        if self.next_spawn_time <= time.absolute_time_seconds() {
            let enemy_prefab: Handle<Prefab<EnemyPrefabData>> = self.enemy_prefab;

            let enemy = entities
                .with(enemy_prefab) // Fails here, required second argument

            // Set new entity position
            let transform = transforms.get_mut(enemy).unwrap();                
            let x = rand::thread_rng().gen_range(0, constants::ARENA_WIDTH);
            transform.set_xyz(x, constants::ARENA_HEIGHT, 0.0);

Followup: If I somehow manage to create an entity from prefab in my code is it possible to fetch its Transform and change position as I tried to do in my code snippet? If not how can I achieve this?

All hints are welcome!

1 Like

You should use WriteStorage<'s, Handle<Prefab<EnemyPrefabData>>>.
But I have no idea how to init a system with a Handle<Prefab<EnemyPrefabData>>, you probably want something like this:

1 Like

Hmmm… I can get this loaded up in code, but the prefab asset never finishes loading. @Vessd any idea why that might be?