Typed VS Packed array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jch

In Godot 4, what is the advantage of using typed arrays, e.g. var x: Array[int] over a packed array, e.g. var x: = PackedInt64Array() ?

:bust_in_silhouette: Reply From: omggomb

A typed array can hold any type available in GDScript, for example a Array[Image] is possible. Packed arrays are only available for exactly the types listed in the API references, (color, int, float, vector, string and byte).

Packed arrays “Pack[…] data tightly […]”, so it’s a memory and possibly performance optimization. But as always you should avoid premature optimization and focus more on getting features done. If a typed array turns out to be a bottleneck it might be worth replacing it with a packed array, but in any case you need to measure performance and memory usage before and after making such a change in order to know if changing it actually helped or made things even worse.

Ah, good point, thanks! Looks like even new classes defined in gdscript can be used for typing, which is great. Reading the docs, I think it might be worth adding one more thing to your answer: Packed arrays do not necessarily include all the methods typed arrays do. For example, pop_back() or pick_random().

jch | 2022-12-18 19:17