Correct declaration for empty Array

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

Please clarify me this once for all:

what is the correct way to declare an empty Array and a pointer to an Array?

Seen that i am low level programmer, sometimes ,even if i am using GDScript since almost 1 year, i come out with those huge doubts :slight_smile:

What is the difference between:

var MyArray: Array


var MyArray = []

And why if i do this:


using the first declaration it raise a warning(and the 2nd not) ??

Both should declare a POINTER to an Array and when i do .append() it should allocate it…Is that right?

Thank you in advance

The first line of code tells the GDScript parser that lives in the editor parsing your code in real-time as you code that you have a variable called MyArray which is a type of Array. That is all it says. It does not initialize an array in memory. It also does not actually prevent you from violating strict typing. This kind of type hint is only helpful to you in the editor (right now), because it gives you an easy way to remember what variables are and what data/methods they have available while you are coding with them.

The second line declares a variable and initializes an Array. The editor’s GDScript parser doesn’t really know what it is. You could write var MyArray := [] to get the type hints back. This is “inferred” type hinting, and only works when whatever you’re assigning to the variable has an easily predictable type. In more indeterminate cases, you have to write them like this: var MyArray: Array = []

The third line creates a warning after the first because the first does not actually go to memory to create an Array. Hope that helps :slight_smile: EDIT: should have been an answer but I made it a comment sorry

DDoop | 2021-01-28 16:41

Thanks for the answer,but i am still confused because i don’t really think what you say it’s 100% correct; for example the editor you mention does not make sense when you compile a build,am i wrong?

Anyway, if i do:

var MyArray: Array

This should raise AN ERROR and not a warning,if what you said it’s correct.

Also doing:

var MyArray: Array

also should raise an error or a violation during runtime.
Instead,everything it’s running fine because (i think but i did not verify the source code) it declare a pointer to an array and when you do append() it just allocate the array.

That’s why i am confused and i want to know HOW TO INIZIALIZE an empy array in the correct way.
All i want it’s to declare an empty Array so i should always use var MyArray = ?

That’s why in C pointers do exists…to make life easier (even if it just APPEAR harder) :slight_smile:

StefanoGrossiNature | 2021-01-28 18:00

I just ran a demo script to verify, and you are correct. var MyArray: Array seems to initialize the variable. That said, I know this kind of type hinting does not improve performance at runtime in Godot like one might expect given how other languages handle typing. This feature is planned but not yet implemented, which is where my confusion came from.

Out of curiosity, I checked the docs page for (what they call – I do not consider it accurate but I’m an apprentice by all means) static typing. It uses the var variable_name: ObjectType convention for declaring and initializing variables quite often (ie: var rifle: Rifle). I think it’s entirely up to you which you would like to use; if you are very passionate about performance right now you could time the alternatives and find out if there is a difference, but I wouldn’t expect one. At the end of the day GDScript is a scripting language and it’s probably best not to overly concern yourself with the low-level implementation except when it rears its head.

Here’s my demo script for anyone curious, just un-comment line 14 to see it break on MyArray0, an uninitialized and un-typed variable.

DDoop | 2021-01-28 18:32

You are very kind dear,i appreciate your intent.
As i am developing a commercial Game i am concerned for sure about performance,but this question is not about performance,what i want to be sure of is that i don’t get access violation when my game come out in different platforms.
I am game developer since 25 years so i have some experience :slight_smile:

I appreciate your help,in absence of other answers i will just use
var MyArray =
for every init i do


StefanoGrossiNature | 2021-01-28 18:55

:bust_in_silhouette: Reply From: backendcoder

I tend to use var MyArray = [] since the meaning is very clear.

Yeah i am doing this too right now :sunglasses:
Seems to be simply the best approach.

StefanoGrossiNature | 2021-01-29 06:14