Menu

Godot Array: GDScript Array Methods with Examples

Everything about arrays in Godot 4: create an array, add and remove items, check size and contents, sort, slice, copy, map, filter and reduce, and 2D arrays for grids.

This page includes runnable editors. Edit, run, and see output instantly.

An Array is an ordered list of values. Write it with square brackets:

["sword", "shield", "potion"]
sword
potion
3
["sword", "bow", "potion"]

Indexes start at 0. A negative index counts from the end. An array can hold mixed types, such as [1, "two", Vector2(3, 4)]. To allow only one type, use a typed array: var inventory: Array[String] = [].

Adding Items

["goblin", "spider", "bat", "slime"]
["goblin", "spider", "bat", "slime", "orc", "troll"]

push_back() is the same as append(). append_array() adds every item of another array.

Removing Items

["dagger", "axe", "sword", "staff"]
["axe", "sword", "staff"]
staff axe ["sword"]
[] true

erase() does nothing when the value is missing. remove_at() with an index past the end prints an error and leaves the array unchanged. pop_back() on an empty array returns null.

Finding Items

true true
1
-1
2
90 40
40 90

Sorting

sort() sorts in place, from small to large. sort_custom() takes a function that returns true when the first argument should come before the second:

[1, 3, 5, 8]
[8, 5, 3, 1]
Bo 300
Ana 120
Chidi 95

a.score works on a dictionary as a shorthand for a["score"] when the key is a valid name.

Slices

slice(begin, end) returns a new array from begin up to, but not including, end:

["tue", "wed"]
["thu", "fri"]
["thu", "fri"]

map, filter, reduce, any and all

These take a function, usually a lambda, and return a result without changing the array:

[24, 10, 60, 16]
[12, 30]
55
true
true

Arrays Are Shared, Not Copied

Assigning an array to another variable does not copy it. Both names point at the same array:

[1, 2, 3, 4]
[1, 2, 3, 4] [1, 2, 3, 4, 5]

The same happens when you pass an array to a function: the function can change the caller's array. Use duplicate() for a separate copy, and duplicate(true) to copy nested arrays and dictionaries too.

2D Arrays

An array of arrays works as a grid. Read a cell with grid[y][x]:

[[0, 0, 0], [0, 0, 7]]
7

Build each row as a new array inside the loop, as above. Appending one row array to every slot would make every row the same shared array, so changing one cell would change a whole column.

A Fixed Size Array

resize() sets the size, filling new slots with null, and fill() sets every slot:

[<null>, <null>, <null>, <null>]
[0, 0, 0, 0]

Quick Reference

TaskMethod
Add at the endappend(x), push_back(x)
Add at the start or at an indexpush_front(x), insert(i, x)
Remove by value or indexerase(x), remove_at(i)
Remove and return the first or lastpop_front(), pop_back()
Length, emptysize(), is_empty()
Contains, positionhas(x), x in a, find(x)
Sortsort(), sort_custom(f), reverse()
Part of the arrayslice(begin, end)
Copyduplicate(), duplicate(true)
A random itempick_random()
Transformmap(f), filter(f), reduce(f, start)

Frequently Asked Questions

How do I add an item to an array in Godot?

Use append() (or its alias push_back()) to add at the end: items.append("key"). push_front() adds at the start and insert(index, value) adds at any position.

How do I remove an item from an array in GDScript?

erase(value) removes the first item equal to the value. remove_at(index) removes the item at a position. pop_back() and pop_front() remove and return the last or first item, and clear() empties the array.

How do I check if an array contains a value in Godot?

Use has() or in: items.has("key") and "key" in items both return true or false. find(value) returns the index, or -1 when the value is missing.

How do I copy an array in Godot?

Call duplicate(). Writing var b = a does not copy: both names point at the same array, so a change through one shows through the other. duplicate(true) also copies nested arrays and dictionaries.

How do I make a 2D array in GDScript?

Make an array of arrays: append one inner array per row, then read a cell with grid[y][x]. For a fixed size grid of tiles, a TileMapLayer or a flat array indexed with y * width + x are common alternatives.

Coddy programming languages illustration

Learn to code with Coddy

Get started