0 votes

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 :-)

What is the difference between:

var MyArray: Array

and

var MyArray = []

And why if i do this:

MyArray.clear()

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

in Engine by (32 points)

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 :) EDIT: should have been an answer but I made it a comment sorry

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

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

Also doing:

var MyArray: Array
MyArray.append()

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) :-)

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.

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 :-)

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

Thanks!

1 Answer

+1 vote

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

by (72 points)

Yeah i am doing this too right now 8-)
Seems to be simply the best approach.

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.