0 votes

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() ?

Godot version 4.0
in Engine by (17 points)

1 Answer

+1 vote
Best answer

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.

by (1,517 points)
selected by

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().

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.