0 votes

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 Engine by (293 points)

2 Answers

+6 votes
Best answer

I can't actually find an official doc for range but it takes minimum, maximum, in steps of x.

for i in range(3, 15, 3):
    #prints 3, 6, 9, 12
by (3,223 points)
selected by

The best range docs I see are on this page. Just search for range:


To be honest it doesn't help that Range is on certain control nodes and range is a function that isn't mentioned in the node documentation page.

I see, okay, I missed the part where range takes up to 3 arguments and when it's does, the last variable is the increments. I appreciate the answer.

Thanks Jgodfrey

And just in case no one sees the description for the range function in the @GDscript documentation, here's a link to it. :-)

0 votes

This answer was completely wrong - removed. :(

by (10,856 points)

When I put

for i in range(2, 8, 2):

in Godot, the debugger prints off 2, then 4, then 6.

Why doesn't it print 2, than 8, than 2?

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.

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 minvalue to maxvalue

range(1,10, 2) would produce 1, 3, 5, 7, 9

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.