#Is it possible to use lifetime in components?

6 messages · Page 1 of 1 (latest)

sly wigeon
#

Hi,
I wanted to store some references inside a component:

pub struct SpriteSheetAnimator<'a> {
    pub animations: HashMap<String, Animation>,
    pub timer: Timer,
    current_frame: u32,
    current_animation_name: Option<String>,
    current_animation: Option<&'a Animation>,
    current_animation_config: Option<&'a AnimationConfig>,
    frame_just_changed: bool
}

But this led me to some lifetime troubles.
I am new to Rust and I love bevy because most of the time I don't need to use lifetime.

The error I am facing is this one, inside a system Query:

error[E0477]: the type `SpriteSheetAnimator<'_>` does not fulfill the required lifetime
   --> src/lib.rs:20:24
    |
20  |     mut sprites_query: &'a Query<'_, '_, (&'b mut SpriteSheetAnimator, &mut Handle<Image>, &mut TextureAtlas)>
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |

Is there a way to easily use lifetime within my use case?

minor quartz
#

Not really, no

#

The only acceptable lifetime here is 'static

full hemlock
#

is Animation and AnimationConfig part of your code, or is it from a crate you are using?

#

if it's from your code, maybe make it an Asset and store the Handle<Animation>