GDQuest / learn-gdscript

Learn Godot's GDScript programming language from zero, right in your browser, for free.
https://gdquest.github.io/learn-gdscript/
Other
2.04k stars 149 forks source link

Lesson 17. Introduction to For Loops: Explain why we need to subtract 1 to board_size.x #955

Open sin3to1faith opened 4 months ago

sin3to1faith commented 4 months ago

In the second example of this section's lesson, board_size.x - 1 is used as a parameter in the range() function when it should be board_size.x as void range(length: int) creates a list of numbers from 0 to length - 1.

This mistake can be seen again in the first practice lesson 'Using a for loop to move to the end of the board'. The user must recreate the mistake shown in this lesson's example to succeed in the practice lesson.

NathanLovato commented 4 months ago

Thanks for taking the time to give feedback. In this case, the lesson's correct. To move to the end of a grid of 5 cells, if you're on cell 1, you need to move 4 times. range(5) (the size of the array) would produce 5 numbers (0, 1, 2, 3, 4) and make you move 5 steps, so 1 cell past the limit. That's why you need to subtract 1 from the board size.

I'm changing the issue title to add a new sentence about that to the lesson.

sin3to1faith commented 4 months ago

if you're on cell 1

I'm guessing cell 1 refers to the 0 on y in Vector2? My apologies for the oversight. I assumed there was a "cell 0" and forgot that it was just an index.

Is it correct to presume that we can use range(board_size.x) if the robot is not already on the board?