Menu

GDScript Packed Arrays: PackedVector2Array and More

Packed arrays store one type tightly in memory: PackedInt32Array, PackedFloat32Array, PackedStringArray, PackedVector2Array and PackedByteArray. When to use them, how they differ from Array, and the overflow trap.

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

A packed array is a list of one built-in type, stored tightly in memory. Godot uses them wherever it handles many values at once, such as the points of a polygon:

[(0.0, 0.0), (100.0, 0.0), (50.0, 80.0), (0.0, 80.0)]
4 points, the second is (100.0, 0.0)

You index, append, loop and check size() the same way as with an Array.

The Packed Array Types

TypeEach item
PackedByteArraya byte, 0 to 255
PackedInt32Array, PackedInt64Arraya 32-bit or 64-bit whole number
PackedFloat32Array, PackedFloat64Arraya 32-bit or 64-bit float
PackedStringArraya String
PackedVector2Array, PackedVector3Array, PackedVector4Arraya vector
PackedColorArraya Color

Where You Meet Them

Many engine properties and methods use packed arrays:

  • String.split() returns a PackedStringArray.
  • Line2D.points, Polygon2D.polygon and CollisionPolygon2D.polygon are PackedVector2Arrays.
  • A navigation path from NavigationServer2D.map_get_path() is a PackedVector2Array.
  • File and network data is a PackedByteArray, for example FileAccess.get_file_as_bytes().
["fire", "ice", "poison"] true
fire + ice + poison
[72, 105] Hi

Packed Array, Array or Typed Array?

ArrayArray[Vector2]PackedVector2Array
Item typesanyVector2 onlyVector2 only
Memorymostmostleast
map(), filter(), pick_random()yesyesno
sort(), has(), find(), slice()yesyesyes
Accepted by engine APIs that want pointsconvertedconverteddirectly

A good rule: use a typed array for game objects and small lists, and a packed array for many plain values or when an engine API asks for one. Convert between them with the constructors:

[30, 10, 20]
[1, 2, 3]

Packed Arrays Are Shared

In Godot 4, a packed array is passed by reference, like an Array. Two variables can point at the same one:

[1, 2, 3, 99]
[1, 2, 3, 99] [1, 2, 3, 99, 4]

The Overflow Trap

Each item has a fixed size, so a value too big for it wraps around without an error:

[44, 5]
[-1294967296]

Use PackedInt64Array for big whole numbers and PackedFloat64Array when 32-bit floats lose too much precision.

Frequently Asked Questions

What is a packed array in Godot?

A packed array stores values of one built-in type side by side in memory, with no per-item overhead. PackedVector2Array holds Vector2 values, PackedStringArray holds strings, and so on. They use less memory than an Array and many engine functions take or return them.

When should I use PackedVector2Array instead of Array[Vector2]?

Use PackedVector2Array for many points, and wherever the engine expects one: Polygon2D.polygon, Line2D.points, Curve2D baked points and navigation paths. Use Array[Vector2] when you want the full set of Array methods such as map() and filter().

Are packed arrays passed by reference in Godot 4?

Yes. Assigning a packed array to another variable or passing it to a function shares it, like an Array. Call duplicate() when you need a separate copy.

Why did my number change in a PackedByteArray?

Each slot is one byte, 0 to 255, so a bigger value wraps around: 300 is stored as 44. PackedInt32Array wraps past about 2.1 billion. Use PackedInt64Array for larger whole numbers.

Coddy programming languages illustration

Learn to code with Coddy

Get started