Update gdscript_basics.rst (#5873)

* Update gdscript_basics.rst

Added some pragmatic advice for for-loops

Co-authored-by: Max Hilbrunner <mhilbrunner@users.noreply.github.com>
This commit is contained in:
NationalityNZ
2022-10-11 03:32:13 -04:00
committed by GitHub
parent f036086dc4
commit 24770fd215

View File

@@ -1143,6 +1143,24 @@ in the loop variable.
for i in 2.2:
statement # Similar to range(ceil(2.2)).
If you want to assign values on an array as it is being iterated through, it
is best to use ``for i in array.size()``.
::
for i in array.size():
array[i] = "Hello World"
The loop variable is local to the for-loop and assigning to it will not change
the value on the array. Objects passed by reference (such as nodes) can still
be manipulated by calling methods on the loop variable.
::
for string in string_array:
string = "Hello World" # This has no effect
for node in node_array:
node.add_to_group("Cool_Group") # This has an effect
match
^^^^^