+6 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 (38 points)

1 Answer

+7 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 (18,211 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

https://docs.godotengine.org/en/stable/classes/index.html

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.

Categories