Skip to reading
GDScript Programming & Godot

11. Arrays and typed arrays

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
Code example
func run() -> Array:
    var clues: Array[String] = ["key", "note"]
    clues.append("watch")
    clues[1] = "torn_note"
    clues.erase("key")
    return [clues, clues.size(), clues.has("watch")]

The first index is 0. Check size before reading an index. Array[String] restricts entries to strings. append adds at the end; insert adds at an index; erase removes a matching value; remove_at removes by index.

Code example
func run() -> Array:
    var original: Array = [{"id": "key"}]
    var independent: Array = original.duplicate(true)
    independent[0]["id"] = "note"
    return [original[0]["id"], independent[0]["id"]]

Arrays are shared by reference. Assigning an array to a second variable does not create an independent list. duplicate(true) copies nested arrays/dictionaries too; contained Godot objects and resources still need their own deliberate ownership/copy policy.

Other useful methods include find, sort, reverse, shuffle, slice, filter, map and reduce. Avoid removing entries from the same array you are walking forward through; collect removals, loop backwards, or build a filtered result.

Page 19 of 378Permalink
Go to page