# Declare Metal Array
var soMetal = ["Metallica", "Rammstein", "Slipknot", "Megadeth"]
The code above shows a GDScript array named
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])
** 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])
** 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)
** 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 **