Browse Source

Update gdscript_basics.rst (#5873)

* Update gdscript_basics.rst

Added some pragmatic advice for for-loops

Co-authored-by: Max Hilbrunner <[email protected]>
NationalityNZ 2 years ago
parent
commit
24770fd215
1 changed files with 18 additions and 0 deletions
  1. 18 0
      tutorials/scripting/gdscript/gdscript_basics.rst

+ 18 - 0
tutorials/scripting/gdscript/gdscript_basics.rst

@@ -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
 ^^^^^