Home Blog Gaming

GDScript Array Functions

Godot Tutorials > GDScript Array Functions

GDScript includes helpful pre-defined array functions. In this lesson I will go over some of the most popular ones. For a full list check out the GDScript Documentation

Append: Add an Item at the End of an Array

extends Node

# Declare an array of strings: card names
var cards = ["paladin", "mage","troll","druid"]

func _ready():
  # add an item "unicorn" at the end of the cards array
  cards.append ("unicorn")
  # Print cards array
  print ("cards array: ", cards)

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
cards array: [paladin, mage, troll, druid, unicorn]
** Debug Process Stopped **

Size: Returns the Number of Items in an Array

extends Node

# Declare Array of strings: card names
var cards = ["paladin", "mage","troll","druid"]
# Declare size: Number of items in the cards array
var size = cards.size()

func _ready():
  # print the number of items in cards
  print (size)

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
4
** Debug Process Stopped **

Shuffle: Changes the Order of Items in an Array

extends Node

# Declare Array of strings: card names
var cards = ["paladin", "mage","troll","druid"]

func _ready():
  # Randomize the Shuffle
  randomize()
  # Shuffle Cards
  cards.shuffle()
  # Print Cards
  print (cards)

  # Shuffle Cards Again
  cards.shuffle()
  # Print Cards Again
  print (cards)

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
[mage, druid, paladin, troll]
[troll, druid, mage, paladin]
** Debug Process Stopped **

Back: Returns the Last Item in an Array

extends Node

# Declare Array of strings: card names
var cards = ["paladin", "mage","troll","druid"]

func _ready():
  # Print the last item in cards
  print(cards.back())

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
druid
** Debug Process Stopped **

Count: Returns the Number of Times Something Appears in an Array

extends Node

# Declare Array of strings: card names
var cards = ["paladin", "mage","troll","druid"]

func _ready():
  # Print the number of times "paladin appears in cards
  print(cards.count("paladin"))
  # Add another Paladin card
  cards.append("paladin")
  # Print the number of times "paladin appears in cards
  print(cards.count("paladin"))

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
1
2
** Debug Process Stopped **

Find: Does an Item Appear in the Array?

Returns 1 for yes and -1 for no.

extends Node

# Declare Array of strings: card names
var cards = ["paladin", "mage","troll","druid"]

func _ready():
  # Does "mage" appear in cards?
  print(cards.find("mage"))
  # Does "wolf" appear in cards?
  print(cards.find("wolf"))

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
1
-1
** Debug Process Stopped **

Homework

Use GDScript code to solve the following problems:
  1. You are making a role play battle card game. Create an array that stores card types: Paladin, Mage, Knight, Troll, Priest
  2. Create a function that takes in your card array as a parameter. The function should shuffle the cards. After the cards are shuffled remove the cards indexed at 0 and 1. Then search the array for the Priest card. If the card exists print a statement letting the user know it was found and also tell the card's index. If priest does not exist, print "Sorry bro, I couldn't find it."
  3. Print the last element in your card array.

Need Homework Help?

Donate

Linux Gaming Blog - Coding Commanders
Palm Beach Techie - Diverisity in Tech
Twitch: daisychaincosplay Live Coding!!!
Hatnix - King of Linux Gaming
murks - Linux Game Dev
Women in Tech - Milly Berst
Women in Tech - Robyn Silber
Women in Tech - she_who_codes
jookia - DOS Bots
ccStack - Linux, NodeJS, ExpressJS, SQL
Learn ReactJS VR on Linux with Commander Candy
Virtual Private Network with Commander Candy
ZED Linux Review by Hatnix
LAMP - Linux, Apache 2, MySQL, PHP
Previous Lesson | Next Lesson