Browse Source

describe negative indexing

implemented in PR #4861
Dennis Brakhane 9 years ago
parent
commit
dc589e1766
1 changed files with 6 additions and 3 deletions
  1. 6 3
      reference/gdscript.rst

+ 6 - 3
reference/gdscript.rst

@@ -415,14 +415,17 @@ Container built-in types
 
 Generic sequence of arbitrary object types, including other arrays or dictionaries (see below). 
 The array can resize dynamically. Arrays are indexed starting from index ``0``.
+Starting with Godot 2.1, indices may be negative like in Python, to count from the end.
 
 ::
 
     var arr=[]
     arr=[1, 2, 3]
-    var b = arr[1] # this is 2
-    arr[0] = "Hi!" # replacing value 1 with "Hi"
-    arr.append(4)  # array is now ["Hi", 2, 3, 4]
+    var b = arr[1]            # this is 2
+    var c = arr[arr.size()-1] # this is 3
+    var d = arr[-1]           # same as the previous line, but shorter
+    arr[0] = "Hi!"            # replacing value 1 with "Hi"
+    arr.append(4)             # array is now ["Hi", 2, 3, 4]
 
 GDScript arrays are allocated linearly in memory for speed. Very
 large arrays (more than tens of thousands of elements) may however cause