Home Blog Gaming

GDScript Arrays

Godot Tutorials > GDScript Arrays

  What is an array?

An array is a data type that holds a set of variables.


  Array Key

A key points to a particular value in an array. We use it to access the value it points to.


  Index Array

If you do not specify the names of your keys, they will automatically be indexed using numbers. The first value in the set with have an index of 0.

  Examples

Say we have an array that stores a list of metal bands...
# Declare Metal Array
var soMetal = ["Metallica", "Rammstein", "Slipknot", "Megadeth"]

The code above shows a GDScript array named soMetal and it stores a list of metal bands: Metallica, Rammstein, Slipknot, Megadeth. We can access the bands by key. The keys of items start with 0 and up in consecutive order (0,1,2,3...).

Mouse Hover over the blue text in the description to highlight the code.

# Declare Metal Array
var soMetal = ["Metallica", "Rammstein", "Slipknot", "Megadeth"]

➼ Key is 0
➼ Key is 1
➼ Key is 2
➼ Key is 3

Now let's say we want to print "Metallica". We can do so by referencing its key. Check out the code below.

# Declare Metal Array
var soMetal = ["Metallica", "Rammstein", "Slipknot", "Megadeth"]

func _ready():
 # Print Metallica 
 print (soMetal[0])

Output

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

Now let's say we want to acces "Rammstein"

# Declare Metal Array
var soMetal = ["Metallica", "Rammstein", "Slipknot", "Megadeth"]

func _ready():
# Print Rammstein 
print (soMetal[1])

Output

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

Now let's say we are building a battle card game and we are declaring character skill arrays.

# Declare Skill Arrays
var paladinSkills = [20,5,15,18,5,10]
var knightSkills = [13,15,10,5,15,10]

➼ Intelligence: Key is 0
➼ Strength: Key is 1
➼ Dexterity: Key is 2
➼ Spirituality: Key is 3
➼ Brutality: Key is 4
➼ Diplomacy: Key is 5

Now let's rewrite our healing code from GDScript If Statements. Instead of declaring brutality and spirituality as seperate variables, we will access the data from our skill arrays. We will then print the healing points of a paladin and the healing points of a knight.

extends Node

# Declare Arrays
var paladinSkills = [20,5,15,18,5,10]
var knightSkills = [13,15,10,5,15,10]

# Caculate base scores
var paladin_baseScore = paladinSkills [3] - paladinSkills[4]
var knight_baseScore = knightSkills [3] - knightSkills[4]

# Initalize healing scores
var paladin_healing: int = 0
var knight_healing: int = 0

func _ready():
  # Calculate Paladin Healing
  if paladin_baseScore > 10:
    paladin_healing += (paladin_baseScore / 5)

  # Calculate Knight Healing
  if knight_baseScore > 10:
    knight_healing += (kinght_baseScore / 5)

  # print healing points
  print ("Paladin Healing: ", paladin_healing)
  print ("Knight Healing: ", knight_healing)

Output

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

Homework

Write GDScript code to perform the following:
  1. Create a skill array (like the ones from the examples) for a mage.

  2. Calculate a mage's healing points

  3. Compare a mage's healing points to a paladin's healing points and output a message telling the user who has more healing points.

Support Coding Commanders

Because it is crucial to achieving upward economic mobility, Coding Commanders believes everyone should have access to free quality STEM education. My tutorials explain complex concepts in plain English using relatable examples. No previous mathematics or technical knowledge required!



Donate
Linux Gaming Blog - Coding Commanders
Palm Beach Techie - Diverisity in Tech
Women in Tech - Milly Berst
Learn HTML on Linux with Commander Candy
Linux Battle Card RPG - Gänseblümchen
Women in Tech - Robyn Silber
ZED Linux Review by Hatnix
Linux (Ubuntu)+ NodeJS + Postgres
LAMP - Linux, Apache 2, MySQL, PHP
Sexy Linux T-shirt
Women in Tech - she_who_codes
Overland on Linux Preview by Hatnix
Previous Lesson | Next Lesson