Menu
CoddyTech

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())
4

An 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 icon

Challenge

Easy

Create 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"]
4

Try it yourself

extends Node

func _ready():
	# Put the four items inside the brackets
	var shopping_list := []
	print(shopping_list)
	print(shopping_list.size())
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online GDScript compiler