#Get sprite size associated with a Component

1 messages · Page 1 of 1 (latest)

clever lintel
#

Hi, I am currently creating a Player component using the following piece of code:

pub fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
    let player_handle = asset_server.load("player.png");
    
    commands.spawn((
        SpriteBundle {
            texture: player_handle,
            ..default()
        },
        Player::default()
    ));
}

and would like to get the sprite's size in another system:

pub fn confine(
  mut player_query: Query<&mut Transform, With<Player>>,
  window_query: Query<&Window, With<PrimaryWindow>>,
 ) {
  // snip
}
#

A short lookup suggested to add assets: Res<Assets<Image>> to the query that implement a get method that requires the corresponding Handle

#

But the issue is the same since I can't access the said handle

#

Hence, how do I solve this issue ?

#

Note: I could tie the handle to the player, but I am not sure that it's the intended way to do it

feral narwhal
#

You can access the handle. Change the query to Query<&Handle<Image>, With<Player>>.
Then you have the handle, and can get the asset with it.