mirror of
https://github.com/stan220/godot-docs.git
synced 2026-09-08 18:29:11 +00:00
classref: Sync with current master branch (c09d83010)
This commit is contained in:
+16
-15
@@ -295,33 +295,34 @@ Output in the console would look something like this:
|
||||
|
||||
- :ref:`Array<class_Array>` **range** **(** ... **)** |vararg|
|
||||
|
||||
Returns an array with the given range. Range can be 1 argument ``N`` (0 to ``N`` - 1), two arguments (``initial``, ``final - 1``) or three arguments (``initial``, ``final - 1``, ``increment``). Returns an empty array if the range isn't valid (e.g. ``range(2, 5, -1)`` or ``range(5, 5, 1)``).
|
||||
Returns an array with the given range. :ref:`range<class_@GDScript_method_range>` can be called in three ways:
|
||||
|
||||
Returns an array with the given range. ``range()`` can have 1 argument N (``0`` to ``N - 1``), two arguments (``initial``, ``final - 1``) or three arguments (``initial``, ``final - 1``, ``increment``). ``increment`` can be negative. If ``increment`` is negative, ``final - 1`` will become ``final + 1``. Also, the initial value must be greater than the final value for the loop to run.
|
||||
\ ``range(n: int)``: Starts from 0, increases by steps of 1, and stops *before* ``n``. The argument ``n`` is **exclusive**.
|
||||
|
||||
\ ``range()(/code] converts all arguments to :ref:`int<class_int>` before processing.
|
||||
[codeblock]
|
||||
print(range(4))
|
||||
print(range(2, 5))
|
||||
print(range(0, 6, 2))
|
||||
\ ``range(b: int, n: int)``: Starts from ``b``, increases by steps of 1, and stops *before* ``n``. The arguments ``b`` and ``n`` are **inclusive** and **exclusive**, respectively.
|
||||
|
||||
Output:
|
||||
\ ``range(b: int, n: int, s: int)``: Starts from ``b``, increases/decreases by steps of ``s``, and stops *before* ``n``. The arguments ``b`` and ``n`` are **inclusive** and **exclusive**, respectively. The argument ``s`` **can** be negative, but not ``0``. If ``s`` is ``0``, an error message is printed.
|
||||
|
||||
\ :ref:`range<class_@GDScript_method_range>` converts all arguments to :ref:`int<class_int>` before processing.
|
||||
|
||||
\ **Note:** Returns an empty array if no value meets the value constraint (e.g. ``range(2, 5, -1)`` or ``range(5, 5, 1)``).
|
||||
|
||||
Examples:
|
||||
|
||||
::
|
||||
|
||||
[0, 1, 2, 3]
|
||||
[2, 3, 4]
|
||||
[0, 2, 4]
|
||||
print(range(4)) # Prints [0, 1, 2, 3]
|
||||
print(range(2, 5)) # Prints [2, 3, 4]
|
||||
print(range(0, 6, 2)) # Prints [0, 2, 4]
|
||||
print(range(4, 1, -1)) # Prints [4, 3, 2]
|
||||
|
||||
To iterate over an :ref:`Array<class_Array>` backwards, use:
|
||||
|
||||
::
|
||||
|
||||
var array = [3, 6, 9]
|
||||
var i := array.size() - 1
|
||||
while i >= 0:
|
||||
print(array[i])
|
||||
i -= 1
|
||||
for i in range(array.size(), 0, -1):
|
||||
print(array[i - 1])
|
||||
|
||||
Output:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user