#Returning EntityCommands from a function

1 messages · Page 1 of 1 (latest)

wicked agate
#

I'm trying to make a helper function for spawning ui that takes in a &mut ChildBuilder and returns an EntityCommands so that i can use it to add other components. here's the important part of the code:

fn button<'a>(parent: &'a mut ChildBuilder<'a>, ...) -> EntityCommands<'a> {
    ...
    let mut entity_commands = parent.spawn(ButtonBundle {
        ...
    });
    ...

    entity_commands
}

this itself compiles but when i try to call it, i get the error that "1 must outlive 2". anyone know what's going on?

carmine creek
#

@wicked agate I'm far from a borrow checker expert but an intuition: can you try removing the 'a after parent: &?

wicked agate
carmine creek
#

How about fn button(parent: &mut ChildBuilder<'_>, ...) -> EntityCommands {?
I remember I had a similar problem when porting a project to Bevy 0.13 and I actually had to remove the lifetimes like this ><"

wicked agate
#

oh yeah that seems to have worked

#

did something slightly different: fn button<'a>(parent: &'a mut ChildBuilder<'_>, display: ButtonDisplay, bundle: impl Bundle) -> EntityCommands<'a>

#

thanks for the help :)

carmine creek
#

No pb!

#

I think the "not implied" solution would be to have a lifetime 'b that you put in &'a mut ChildBuilder<'b> and specify 'a to be shorter than 'b, maybe? But when I tried it didn't seem to work, I think the '_ does that implicitly

wicked agate
#

probably something i'll look into after i've finished this project

velvet igloo
#

bevy_quickstart seems to be able to do this without any lifetimes. Not sure what's different. See widget.rs.

wicked agate
#

ohh that’s really interesting

#

i’ll give that a look tomorrow