-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays.gd
72 lines (57 loc) · 2.48 KB
/
arrays.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# this script extends Node2D because it is on a Node2D node
extends Node2D
# the function "_ready" is called at the beginning of the game
func _ready():
# this allows you to select what node you want to see output from
if not visible:
return
print("---Array node output---")
# an array is an ordered list of variables
# you can create one by listing all the values inside square brackets like this:
var array = [0, 12, 5, 2, "cheese"]
# in many programming languages all of the items in an array have to be the same type
# but GDScript allows for variables of different types to be in the same array
# you can get the variables in an array by putting a number in square brackets after the array
# for example
print("-indexing-")
print(array[1])
# this is referred to as "indexing" the array, and the number is called the "index"
# the first item in an array has an index of 0 NOT 1, so the above statement prints "12" not "0"
# variables can also be used as the index, for example
var index = 12 / 4
print("-indexing with variable-")
print(array[index])
# this prints array[3] which is 2
# you can add items on to the end of an array using the "append" function
array.append(7)
# you can get the size of the array by using the "len" function on it
print("-length-")
print(len(array))
# this prints "6" because the array originally had 5 items, but we added one to it with "append"
# because the array's indices start at 0, the last item is length - 1
# for example, this prints the last item in the array: 7
print("-last item-")
print(array[len(array) - 1])
# you can insert items into the middle of an array using the "insert" function
# insert takes an index and a value
array.insert(4, "swiss")
# the value in the array at the index given becomes the value given in the "insert" function
# the array used to look like this
# [0, 12, 5, 2, "cheese"]
# but now it looks like this
# [0, 12, 5, 2, "swiss", cheese"]
# we can verify this by using print
print("-insert-")
print(array[4]) # prints "swiss"
print(array[5]) # prints "cheese"
# there are a bunch of other functions you can use on arrays, they can be found online
# or by ctrl-clicking the word "Array"
# a for loop can be used to go through an array
# this prints all the items in the array
print("-items-")
for item in array:
print(item)
# if you want to have the index as well, you can do this
print("-item with indices-")
for i in range(0, len(array)):
print("index: ", i, " item: ", array[i])