[4.7 Stable] - How To Do "Static Typing" For Arrays?

Godot Version

v4.7.stable.official [5b4e0cb0f] - Linux x86_64

Question

Hi,

Trying to improve performance now.
How do we do “Static Typing” for Arrays?
Let us know, thanks!

SS

var my_array: Array[YourTypeGoesHere]

There are also a few compact array types, these have less helper functions but can be faster than the basic Array type

var my_array: PackedByteArray
var my_array: PackedInt32Array
var my_array: PackedVector2Array

I recommend also checking this page about Arrays, it has a little more information, expanding on what @gertkeno said.

Hi,

How do we do the above with multi dimensional Arrays?

For example:

var PieceData = []

func InitializePieceData():
PieceData.resize(8)
for piece in range(8):
PieceData[piece] = []
PieceData[piece].resize(5)
for rotations in range(5):
PieceData[piece][rotations] = []
PieceData[piece][rotations].resize(17)

SS

You generally don’t, you can specify that you expect an array inside of an array, but not further

Array[Array] # valid type
Array[Array[int]] # invalid, too deep

What if you did:

var outer_array : Array[Array]
var inner_array : Array[int]

outer_array[0] = inner_array

Would that work?

Yes but it would lose it’s typing, demote down to Array

there are no matrices in Godot. You can use a Dictionary, but that’s bad for performance (well it depends).

A Matrix is just the way to access data in a Vector, What I do is create a new Resource or Singleton to handle this from methods or properties:

singleton with methods:

var arr Array : int = []
var m_size = 16#this is up to your array dimensions

#needs more checks
func get_val(x : int, y : int) -> int:
	#check out of bounds.
	if x > -1 and y > -1:
		if len(arr) > x * y:
			return arr[(y * m_size) + x]
		else:
			return arr[0]
	else:
		return arr[0]

func set_val(x : int, y : int, value : int) -> void:
	if x > -1 and y > -1:
		if len(arr) > x * y:
			arr[(y * m_size) + x] = value

edit:

you can use a Resource to store data, that would allow for static typing. I don’t know about the performance though:


class_name Piece
extends Resource

var rotations : Array[int]
var piece_data : Array[Piece]

Doesn’t need to be a Resource, can be a RefCounted too

RefCounted should not be used as it is not memory safe.

The lowest type that should be used IS Resource.

I’d love to read more about this since as far as I know, RefCounted was specifically created to provide automatic memory management and safety. So as far as my knowledge goes, it’s completely memory safe.

Use C# :wink:

You might be right. I was remembering this warning from the RefCounted documentation:

RefCounted instances caught in a cyclic reference will not be freed automatically. For example, if a node holds a reference to instance A, which directly or indirectly holds a reference back to A, A’s reference count will be 2. Destruction of the node will leave A dangling with a reference count of 1, and there will be a memory leak. To prevent this, one of the references in the cycle can be made weak with @GlobalScope.weakref().

That is A solution I guess. I just personally don’t like C# and there are other people who are in the same boat, it also adds complications like having to install dotnet and having to compile every time, and needing an external editor.

All valid points, I was being slightly tongue in cheek.

It does however have far more efficient collections, so if thats important, and you dont need those collections exported to the editor, C# would be preferred (if certain features and speed is important).

The same is true for Resource, because it extends RefCounted, so suggesting one over the other based on this argument is pointless.

I would still suggest to use Resource as a default choice over RefCounted, because the memory and performance difference between the two is miniscule and negligible in most cases, but Resource allows serialization out of the box and can be exported in the Inspector, and you never know when you’re gonna need it. Use RefCounted only if you need that small extra performance.

But this discussion is kinda getting off topic, which was about Arrays, so if someone wants to argue the Resource/RefCounted subject - feel free to open a new topic.