#how to make a component display a sprite

4 messages · Page 1 of 1 (latest)

pulsar snow
#

i have a class of Person with a name and a texture that is a spritebundle but the struct doesnt wanna display the sprite in it?

use bevy::prelude::*;

#[derive(Component)]
struct Person{
    name:String,
    textur: SpriteBundle
}

fn main() {
    App::new()
      .add_plugins(DefaultPlugins)
      .add_systems(Startup, setup)
      .run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(Camera2dBundle::default());

    commands.spawn(Person {
        name: "cheesecake".to_string(),
        textur: SpriteBundle { texture: asset_server.load("sprites\\tile1.png"), transform: Transform::from_xyz(0f32, 0f32, 0f32), ..default()}
    });

    commands.spawn(SpriteBundle {
        texture: asset_server.load("sprites\\tile2.png"),
        transform: Transform::from_xyz(100f32, 0f32, 0f32),
        ..default()
    });

}
tough bronze
#

Hi, bevy expects mesh, texture, etc handles, etc added by SpriteBundle and similar bundles to be directly on the entity. Bevy doesn't know that it is in your component and doesn't how to extract it from your component.
So you have 2 options:

  1. Create PersonBundle bundle which will have Person component and SpriteBundle bundle.
  2. I think it would be possible to create custom extract logic for the Person component...probably

I recommend 1. approach

pulsar snow
#

testing it rn

#

yuss it works