Skip to content

How to cleanly spawn simple players #18770

Answered by Henauxg
wazawoo asked this question in Q&A
Discussion options

You must be logged in to vote

For the initial color of the material, the simplest way to achieve what you want would be to simply initialize the Material with the correct value when spawning the player entity:

fn spawn_players(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    // Added
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
   // ...
   
   commands.spawn((
        player_one.clone(),
        player_one.initial_transform,
        Mesh2d(meshes.add(Rectangle::default())),
        // Added
        MeshMaterial2d(materials.add(player_one.color)),
    ));
    
    // ...

If you could not do this for some reason, or wanted to defer the color assignment to your players. You could use Obser…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@wazawoo
Comment options

Answer selected by wazawoo
Comment options

You must be logged in to vote
2 replies
@wazawoo
Comment options

@Person-93
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants