guimcaballero / bevy_chess

Chess demo in Bevy
https://caballerocoll.com/blog/bevy-chess-tutorial/
MIT License
75 stars 17 forks source link

Missing / Out-of-Order Code in Tutorial #5

Closed anthonyarusso closed 3 years ago

anthonyarusso commented 3 years ago

The Issue

Throughout the Bevy engine chess tutorial there are a number of places where the program will not function as described by the tutorial because code required for error-free execution (i.e. variable declarations) was not introduced at the step it should have been.

Specifics

Cannot find type Piece in this scope.

In the tutorial after the paragraph: "We now need to implement something to change the unit positions. We'll change the select_square system to also move the pieces for now, and we'll refactor it into something neater later:" the following code is displayed:

fn select_square(
    pick_state: Res<PickState>,
    mouse_button_inputs: Res<Input<MouseButton>>,
    mut selected_square: ResMut<SelectedSquare>,
    mut selected_piece: ResMut<SelectedPiece>,
    squares_query: Query<&Square>,
    mut pieces_query: Query<(Entity, &mut Piece)>,
) {

At this point, you will receive the error "Cannot find type Piece in this scope.".

Solution: change the PieceColor, PieceType, and Piece structs to all be public and add use crate::pieces::* to the top of board.rs. The tutorial shows neither of these steps, but the final code does.

Cannot find value pieces_vec in this scope.

In the tutorial after the paragraph: "Finally, we just need to call the function before moving a piece to check that the move is valid:" the following code is displayed:

fn select_square(
                [...]

                // Move the selected piece to the selected square
                if let Ok((_piece_entity, mut piece)) = pieces_query.get_mut(selected_piece_entity)
                {
                    if piece.is_move_valid((square.x, square.y), pieces_vec) {
                        piece.x = square.x;
                        piece.y = square.y;
                    }
                }

                [...]
}

At this point, you will receive the error "Cannot find value pieces_vec in this scope.".

Solution: add let pieces_vec = pieces_query.iter_mut().map(|(_, piece)| *piece).collect(); under the line if let Ok((_piece_entity, mut piece)) = pieces_query.get_mut(selected_piece_entity).

Note: The tuple inside the .map(| |) closure and Ok(()) lines only contains two arguments as the children argument has not been introduced by this point in the tutorial.

Field 0 of struct bevy::prelude::Children is private

In the tutorial after the paragraph: "Here's the new function that also takes care of despawning a piece after it's taken:" the following code is displayed:

...
 let pieces_entity_vec: Vec<(Entity, Piece, Vec<Entity>)> = pieces_query
                    .iter_mut()
                    .map(|(entity, piece, children)| {
                        (
                            entity,
                            *piece,
                            children.0.iter().map(|entity| *entity).collect(),
                        )
                    })
                    .collect();
...

At this point, you will receive the error "field 0 of struct bevy::prelude::Children is private" on the children.0.iter()... line.

Solution: Change children.0.iter() to children.iter(). This solution I don't understand as much since I just got it from tubotinatub on the Discord.

That is all. Let me know if I was mistaken or if you need help understanding any of these issues.

Thanks again for the great tutorial. It's really helped me grasp Bevy.

guimcaballero commented 3 years ago

Thanks for being so thorough! I've fixed these issues now.

Last one is due to the update from Bevy 0.3 to 0.4, which changed how Children work, but since in the end we don't use them, I did not realize while updating!