Topic was automatically imported from the old Question2Answer platform.
Asked By
Dumuz
I was looking over the doc’s for using the for syntax and it was making sense until the example showed this line of script:
for i in range(2, 8, 2):
statement # Similar to [2, 4, 6] but does not allocate an array.
Why does this generate these integers?
From the looks of these examples using for in range, it seems like it should iterate through the array as [2, 8, 2]. What math am I not understanding here?
in Godot, the debugger prints off 2, then 4, then 6.
Why doesn’t it print 2, than 8, than 2?
Dumuz | 2020-02-05 22:56
You can hide your answer by clicking the three vertical dots in the lower right corner of your reply and selecting “Hide”. From the standpoint of other users, this is the same as deleting your answer.
njamster | 2020-02-06 11:27
From how I understand Godot, that looks like the first argument in range() is the minimum value, followed by maximum value, and the 3rd argument simply means the incremental value, so how many steps you want to take from min_value to max_value
for i in range(start_value, ending_value, step_size)
where step_size represents how much the i will increase by each iteration. So in the example provided, the for loop would go from 2 to 8 in increments of 2.
For comparison, when iterating through an array you would use:
for i in [2, 8, 2]:
Note the [] instead of the () used in the range iteration. This would then create the output you were expecting.