Hey guys, here is a quick question.

what will this produce?

var node_letters = ["One","Two","Three"]

A. Array B. PoolStringArray

= =''

From what I can tell from the documentation you should expect an Array and if you want a specialised array you need to define it explicitly.

This is an array

["One","Two","Three"]

This is a PoolStringArray

PoolStringArray(["One","Two","Three"])

As @Kequc said, if you want a specialized array, you will need to define it explicitly when you assign it to the variable.

Also, keep in mind that the default GDScript arrays can contain any variable type, while specialized arrays can only contain a single variable type. You can find more about arrays in GDScript in the documentation.

In the code you posted above, node_letters will be a generic array, filled with strings. If you want a PoolStringArray, or any other custom array datatype, you will need to define it manually.

Personally, unless you need it for performance, I wouldn't recommend using a PoolStringArray, as depending on what you need to do they can be tricky to work with. Another thing to note is that generic arrays are passed by reference, while specialized arrays are passed by value.

Hopefully this helps.

(Side note: I edited the posts on this topic so the code is formatted)

Thats where you're wrong! This is a PoolStringArray

["One","Two","Three"]

Godot does its "own thinking - all string members" and decides to save on processing so it makes it a PoolStringArray.

Why do i care? becuz i have to then convert it to a generic array just so i can access the shuffle method! and you'd think all kinds of array would have a method to covert itself to an Generic Array without me casting it(or going thru the trouble of using the array constructor to create one from a poolstringarray) and hoping it all works out.

@canvasbushi Sounding a bit harsh to me. Just try out following code and you will see that if you not explicitely define the type of an array Godot will just create a generic one.

func _ready():
	var array = ["one", "two", "three"]
	print(typof(array))

Output is 19 which stands for an Array, PoolStringArray would be 23.

try using the shuffle method. btw I'm not sure what you're coding on, but i'm using the latest official build 3.1.1

@canvasbushi said: try using the shuffle method. btw I'm not sure what you're coding on, but i'm using the latest official build 3.1.1

The shuffle functions works fine for me. I'm using Godot 3.1.1 on Windows 10:

Right, I left off the part where i used split. thats what made it into a PoolStringArray. :p

@canvasbushi said: Right, I left off the part where i used split. thats what made it into a PoolStringArray. :p

Well, that would explain a lot. After all, using "one,two,three".splt(",") is completely different from ["one", "two", "three"].

Anyway, you just need to convert the PoolStringArray into a generic array if you want to use the shuffle function. I tested the following code and it shuffles the array as expected:

extends Node2D

func _ready():
	#var array = ["One", "Two", "Three"]
	
	var array = "One, Two, Three".split(", ");
	array = Array(array);
	
	print (array);
	array.shuffle();
	print (array);

The key line is array = Array(array), as it converts the PoolStringArray into a generic array. Hopefully this helps.

This isnt a problem, i was simply annoyed that i had to convert it back to array.

The one advantage of the non-generic array types is that they handle memory better for huge sets of data (ie. thousands of entries).

Otherwise, generic arrays are faster and have a more expansive set of functions.

4 years later