#Proper type for function that returns EntityCommands

3 messages · Page 1 of 1 (latest)

cyan fiber
#

I have this code:

fn build(
        &self,
        transform: Transform,
        commands: &mut Commands,
    ) -> &mut bevy::ecs::system::EntityCommands {
        commands
            .spawn()
            .insert_bundle(self.components[0].spawn(transform))
            .with_children(|parent| {
                for component in &self.components[1..] {
                    &mut parent
                        .spawn()
                         // I actually had the exact same issue with this component.spawn() function but in that case I can just return a bundle since it's used only here. However, this build function needs a lot more uses so that would be impractical. Also returning a bundle won't let me add children
                        .insert_bundle(component.spawn(Transform::default()));
                }
                if let Some((collider, offset)) = self.collider {
                    parent
                        .spawn_bundle(TransformBundle {
                            local: Transform::from_translation(offset),
                            ..default()
                        })
                        .insert(collider.clone());
                }
            })
    }

But I'm getting lifetime errors. What I basically want to do is have a function that creates a component and adds some children and bundles to it, but then also exposes it so the calling code can maybe add more components or children.

Is there a way to either 1) Fix the lifetimes here or 2) expose the ability to add more components in a totally different way

sand rune
elder pewter