+7 votes

Newbie question.. I tried few ways and looks like I did not manage it to work..
Lets say I need Add my objects to an array and wan t manipulate them straight from array. (and not to fetch them temporary to Spatial variable..)

Var myarray:Spatial = [] # no..?
Var my
array= []:Spatial # no..?
Var myarray= [:Spatial] # no..?
Var my
array= [] as Spatial [] #????...not tested..

Or is it even possible

in Engine by (39 points)

1 Answer

+9 votes
Best answer

Type hints can't be used with arrays. The Array container in Godot is a variant container. If you have a particular need for a single-typed array, there are the Pool*Array types.

You can still add Spatial objects to your array, you just can't specify that the array will only hold Spatials.

by (20,485 points)
selected by

Pool array classes:

var my_generic_array: Array
var my_byte_array: PoolByteArray
var my_color_array: PoolColorArray
var my_int_array: PoolIntArray
var my_float_array: PoolRealArray
var my_string_array: PoolStringArray
var my_vector2_array: PoolVector2Array
var my_vector3_array: PoolVector3Array


If anyone's interested, there's a proposal to support statically typed arrays for Godot 4.0:

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 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 webmaster@godotengine.org with your username.