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
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)
** 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 **
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)
** Debug Process Started ** OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2) 4 ** Debug Process Stopped **
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)
** 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 **
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())
** Debug Process Started ** OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2) druid ** Debug Process Stopped **
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"))
** Debug Process Started ** OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2) 1 2 ** Debug Process Stopped **
Returns
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"))
** Debug Process Started ** OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2) 1 -1 ** Debug Process Stopped **