How can I select one item from an array?

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

I want to get one item from an array, for example:

var grocery_list = [eggs, butter, milk]

I want to call the item by index but it doesnt work, I know you can use find or whatever, but thats not what i want to do. I tried things like:


but non of them work
I can’t find anything like that in the godot API either, is it even possible?

:bust_in_silhouette: Reply From: kidscancode

Of course you can access arrays by element, that’s one of the main uses of arrays.

I’m not sure what you mean by it not working.

var grocery_list = ['eggs', 'butter', 'milk']
print(grocery_list[1]) # prints "butter"

There is a section in the GDScript tutorial on arrays here:

And the API docs for array are here, where the available array methods are defined:

:bust_in_silhouette: Reply From: IronStudios

You named the variable grocery_list and you’re trying to access it with grocerylist

var grocery_list = [eggs,butter,milk]

grocery_list[0] #Will return "eggs" (without quotes)
grocerylist[1] #Will return an error

Also, KidsCanCode was right too