I'm currently working through a GDScript tutorial from Godot called 'Learn to Code from Zero' and I'm on lesson 19, which covers looping over arrays. I have a bit of coding background, having taken a Visual Basic course and dabbled with JavaScript and Python, so I grasp the basics pretty well.
In my first practice from this lesson, I'm tasked with moving a robot along a defined path using a `for` loop. The initial code given is:
```gdscript
var robot_path = [Vector2(1, 0), Vector2(1, 1), Vector2(1, 2), Vector2(2, 2), Vector2(3, 2), Vector2(4, 2), Vector2(5, 2)]
func run():
```
I figured out the rest with some hints and the solution, which is:
```gdscript
func run():
for cell in robot_path:
robot.move_to(cell)
```
From my notes, I understand that `robot_path` is an array storing `Vector2` positions, and the loop goes through each `cell` in that array to direct the robot to move to those coordinates. Is this interpretation accurate?
In the next practice, I'm supposed to draw several rectangles using an array of sizes and a loop. The code snippet is as follows:
```gdscript
var rectangle_sizes = [Vector2(200, 120), Vector2(140, 80), Vector2(80, 140), Vector2(200, 140)]
func run():
for size in rectangle_sizes:
draw_rectangle(size.x, size.y)
jump(size.x, 0)
```
I'm confused about how I can use "for size in rectangle_sizes:". Where does this "size" come from? Is it a variable or a label? What exactly is its role in this loop?
2 Answers
Exactly! The `size` variable in the loop is essentially a placeholder for each element in the `rectangle_sizes` array. The loop structure basically follows this pattern: `for variable in collection:` where you define what you want to iterate through. So you have the `for` keyword indicating a loop, and `size` is the iterator that represents each rectangle size in your array during the loop execution—all wrapped in the code that gets carried out while it runs.
Additionally, regarding your question about the turtle drawing rectangles—your interpretation is close! The `draw_rectangle(size.x, size.y)` command uses the dimensions stored in `size` to draw each rectangle. The `jump(size.x, 0)` moves to the next drawing position to ensure the rectangles don't overlap. It processes each size iteratively, allowing accurate placement of each rectangle.
The term `size` is simply a variable name you've chosen for that loop. You can name it anything you like—like `fooplah`—but it helps to use a descriptive name. Since you're looping through the sizes of rectangles, `size` makes the most sense. When you do `for size in rectangle_sizes:`, the loop iterates over each element in `rectangle_sizes`, assigning each one to `size` for the duration of each loop cycle.
As for your first question, you're on the right track with your understanding of how the loop works! Just keep in mind that `Vector2` is a type identifier that helps define 2D coordinates, not just a name. So yes, your logic about the loop seems solid!
Oh, so after I call for, I just specify a variable name? Thanks for clarifying!
Thanks a lot! I kind of missed the part about naming the variable after the `for` keyword. But I get it now about how the drawing works. It seems like it's a step-by-step calling to draw each rectangle clearly!