Hi! I know this is probably silly but I'm doing the loops in the tutorial and I understand that variables in these loops are temporary (I hope I understand that correctly) but here I needed to access the array's vector two coordinates and that was done by using "variable_name(dot)x, variable_name(dot)y" and I don't properly understand why. Is that simply just the way it is? It's clear I must not have understood it before the "Vector2" replacement. Any help or feedback on this is welcome, thanks.
#Tutorial question
6 messages · Page 1 of 1 (latest)
The Vector2 type is just a way to combine two numbers into one container/variable. As a matter of fact, its components are named arbitrarily x and y. But as said in the course, you can use a Vector2 value (the real name is instance) any way you see fit. For example, it can represent a position, a movement, a scaling factor.
In your context, you use a Vector2 value as a representation of a rectangle dimensions.
So, even though you use x and y properties, what it truly mean is width and height.
Let me modify your code so you can better understand.
func run():
# here I replaced the misleading "squares" name by a (singular) "rectangle_size" name
for rectangle_size in rectangle_sizes:
# extract the width and height from the Vector2 value
var width := rectangle_size.x
var height := rectangle_size.y
draw_rectangle(width, height)
jump(300, 0)
One other thing that might be the source of your confusion is the array rectangle_sizes.
It's actually an array containing elements of Vector2 type.
So basically, each element of that array is a Vector2 value representing a single rectangle dimensions (that is, x as its width and y as its height).
Therefore, the array contains multiple Vector2 values, each representing a single rectangle dimensions. This is why the array variable is called rectangle_sizes (plural).
Finally, to be extra explicit, the for loop goes through each element of the array, assigning the current element of the array to the variable name you choose (the one right after the for keyword). So keep in mind that each repetition (we say iteration in programming) of the loop, that variable contains a single element of the array.