Declaring an Array
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 62 of 92.
An array stores several values in one variable, in order. You create it with square brackets [] and separate the values with commas:
var items := [1, "two", 3.5, true]A plain array can hold values of different types, like the int, string, float and boolean above. An empty array is a pair of brackets with nothing inside:
var inventory := []print() shows the whole array, with strings in quotation marks:
var fruits := ["apple", "pear"]
print(fruits)["apple", "pear"]The size() method returns how many elements an array holds. You call it with a dot after the array:
var items := [1, "two", 3.5, true]
print(items.size())4An array can also hold other arrays. size() counts only the top-level elements, so [[1, 2], [3, 4, 5]] has a size of 2.
Challenge
EasyCreate a shopping list.
Put the strings bread, eggs, milk and butter, in this order, inside the brackets of shopping_list. The two print() lines are already written, so the program prints:
["bread", "eggs", "milk", "butter"]
4Try it yourself
extends Node
func _ready():
# Put the four items inside the brackets
var shopping_list := []
print(shopping_list)
print(shopping_list.size())
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators10Functions
Declare a FunctionArgumentsReturnReturn TypesRecap - Sigma FunctionRecap - Validation FunctionDefault Values5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary OperatorPractice on your own: Online GDScript compiler