Arrays | Godot GDScript Tutorial | Ep 10

Godot Tutorials
15 Apr 202011:30

Summary

TLDRThis episode of the GD Script Fundamental Tutorial series delves into the concept of arrays in Godot programming. Arrays are introduced as sequential collections of items stored in contiguous memory locations, capable of holding different data types. The tutorial explains array indexing, including normal and inverse positions, and demonstrates how to declare, manipulate, and access array elements in GDScript. It covers sub-arrays, array methods like push, pop, clear, and duplicate, and the importance of choosing between deep and shallow duplication. The episode concludes with practical examples and encourages viewers to download a GitHub project for hands-on experience, aiming to solidify their understanding of array manipulation in Godot.

Takeaways

  • 📚 Arrays in GDScript are collections of items stored in contiguous memory locations.
  • 🔢 Arrays can contain elements of different data types, including other arrays and dictionaries.
  • 👤 The position of an item in an array is called an index, starting at 0 for the first element.
  • 🔄 You can use both positive and negative indices to access elements in an array, with negative indices accessing elements from the end.
  • 📝 To declare an array in GDScript, use square brackets with values separated by commas.
  • 🔑 Retrieving a specific element from an array requires using the variable name followed by square brackets with the index.
  • 💡 GDScript supports sub-arrays, which are arrays within arrays, and can be multi-dimensional.
  • 🔄 The push method adds elements to the end of an array, while the pop method removes elements from the beginning or end.
  • 🗑️ Clearing an array can be done by reassigning an empty array, using the resize method, or calling the clear method.
  • 🔍 The duplicate method creates a copy of an array, with options for deep or shallow duplication.
  • 📏 The size method returns the length of an array, which is the last index position plus one.

Q & A

  • What is an array in the context of programming?

    -An array is a collection of items stored at contiguous memory locations, allowing for the storage of multiple items together in a sequential manner.

  • Can arrays in Godot contain elements of different data types?

    -Yes, in Godot, arrays can contain elements of different data types, including numbers, strings, and even other arrays and dictionaries.

  • How is the position of an element in an array referred to?

    -The position of an element in an array is referred to as its index position, which starts at 0 for the first element.

  • What is an inverse index position and how is it used?

    -An inverse index position is a way to access elements from the end of an array using negative numbers, with -1 referring to the last element and -2 referring to the second-to-last element.

  • How do you declare an array in Godot's GDScript?

    -In GDScript, you declare an array by using square brackets. An empty array is declared with '[]', and an array with predetermined values is declared by listing the values separated by commas within the brackets.

  • How can you retrieve a specific element from an array?

    -To retrieve a specific element, you type out the variable name followed by square brackets containing the index position of the desired element.

  • What are sub-arrays and how are they created?

    -Sub-arrays are arrays within arrays, also known as multi-dimensional arrays. They are created by using square brackets followed by another pair of square brackets.

  • How do the push and pop methods work in GDScript arrays?

    -The push method adds an element to the beginning or end of an array, while the pop method removes and returns an element from the beginning or end of an array.

  • What is the difference between a deep copy and a shallow copy when duplicating arrays in GDScript?

    -A deep copy duplicates all nested arrays and dictionaries, creating independent copies that do not affect the original. A shallow copy, however, keeps references to the original nested arrays and dictionaries, meaning changes to these nested structures in the copy will also impact the original.

  • How can you get the length of an array in GDScript?

    -To get the length of an array, you call the 'size' method on the array, which returns an integer representing the number of elements in the array.

  • What are the different ways to clear an array in GDScript?

    -There are three ways to clear an array in GDScript: 1) Assigning an empty array to the variable, 2) Using the 'resize' method with a length of 0, and 3) Calling the 'clear' method on the array.

Outlines

00:00

📚 Introduction to Arrays in GDScript

This paragraph introduces the concept of arrays in GDScript, explaining that arrays are a collection of items stored in contiguous memory locations. It uses the analogy of a line of people to illustrate how elements are stored sequentially and accessed by index positions starting from 0. The paragraph also touches on the ability to use inverse index positions for accessing elements from the end of the array. It provides a basic guide on how to declare arrays in GDScript, mentioning that arrays can contain different data types and can be mixed with arrays and dictionaries. The explanation includes how to declare an empty array and one with predetermined values, emphasizing the need for comma separation of elements.

05:01

🔍 Manipulating Arrays: Retrieval, Sub-arrays, and Methods

This section delves into the manipulation of arrays in GDScript, starting with the retrieval of data. It explains how to access the entire array or a specific element using the variable name and index positions. The concept of sub-arrays or multi-dimensional arrays is introduced, demonstrating how to create and access elements within them. The paragraph then covers array methods such as 'push' and 'pop', which are used to add or remove elements from the beginning or end of an array. It also introduces the 'clear' method and the 'resize' method as ways to clear an array, with a preference for the 'clear' method due to its intuitiveness. Additionally, the paragraph discusses the 'duplicate' method for creating copies of arrays, explaining the difference between deep and shallow duplication.

10:03

🔄 Practical Examples and Array Length

The final paragraph provides practical examples of array manipulation in GDScript, including the declaration of arrays, sub-arrays, and accessing values at specific indices. It demonstrates the use of 'push' and 'pop' methods with both front and back operations, as well as the different ways to clear an array. The paragraph also explains how to duplicate an array with a deep copy by setting the boolean value to true in the 'duplicate' method. Lastly, it covers the 'size' method to get the length of an array, which is essential for understanding the array's capacity. The paragraph concludes with an invitation to download a GitHub project for hands-on experience with array manipulation, promising a better understanding through practical demonstration.

Mindmap

Keywords

💡Array

An array is a fundamental data structure used in programming to store multiple items in a single variable. In the context of the video, arrays in Godot are collections of items stored in contiguous memory locations, allowing for efficient access and manipulation. The script explains that arrays can contain elements of different data types, including other arrays, making them versatile for various programming tasks.

💡Index Position

Index position refers to the location of an element within an array. The video script illustrates that arrays use zero-based indexing, meaning the first element is at position 0, the second at position 1, and so on. This concept is crucial for accessing and manipulating specific elements within an array, as demonstrated when the script discusses retrieving data from an array using its index.

💡Element

In the context of arrays, an element is a single value or item stored within the array. The video explains that a value inside an array is called an element, and these elements are accessed via their index positions. For example, the script mentions retrieving a specific element by referring to its index in the array.

💡Sub-array

A sub-array is an array that is nested within another array. The video script introduces the concept of sub-arrays, or arrays within arrays, which can be used to create multi-dimensional data structures. This is demonstrated when the script discusses accessing elements within a sub-array by using a pair of square brackets for each level of nesting.

💡Push Method

The push method is a function used to add elements to an array. The video script explains that in Godot, the push method can be used to add elements to either the beginning or the end of an array. This method is essential for dynamically modifying the size of an array and is illustrated with examples in the script.

💡Pop Method

The pop method is used to remove elements from an array and can also return the removed element. The video script describes the pop method as a way to remove the last element of an array with 'pop back' or the first element with 'pop front', showcasing its use in shrinking an array and retrieving values.

💡Clear Method

The clear method is a function that empties an array of all its elements. The video script discusses three ways to clear an array in Godot: reassigning an empty array to the variable, using the resize method to set the array's length to zero, or calling the clear method directly. This is an important concept for resetting arrays to their initial state.

💡Duplicate Method

The duplicate method is used to create a copy of an array. The video script explains that Godot offers the option of deep duplication, where nested arrays and dictionaries are fully copied, or shallow duplication, where references to the original nested arrays and dictionaries are kept. This distinction is important for understanding how copies of arrays interact with their originals.

💡Deep Copy

A deep copy is a process where an entirely new copy of an array is created, including all nested arrays and dictionaries, without sharing references with the original array. The video script uses the duplicate method with a boolean value of true to demonstrate how to perform a deep copy in Godot, ensuring that modifications to the copy do not affect the original.

💡Shallow Copy

A shallow copy, in contrast to a deep copy, creates a new array but keeps references to the original nested arrays and dictionaries. The video script explains that modifying a sub-array or dictionary in the shallow copy will also impact those referenced in the original array, as demonstrated by the duplicate method with a boolean value of false.

💡Size Method

The size method is used to determine the number of elements in an array. The video script describes the size method as returning an integer representing the length of the array, which is essentially the last index position plus one. This method is fundamental for understanding the dimensions of an array and for iterating through its elements.

Highlights

Introduction to arrays as a collection of items stored in continuous memory locations.

Arrays in Godot can contain elements of different data types, including arrays and dictionaries.

Explaining arrays with the analogy of a line of people, with positions and index positions starting at 0.

The concept of inverse index positions for accessing elements from the end of the array.

Declaring an array in Godot with square brackets and the possibility of mixed data types.

Retrieving data from an array using the variable name and index positions.

Introduction to sub-arrays and multi-dimensional arrays in Godot.

Demonstration of accessing values within sub-arrays using nested brackets.

Description of array methods in Godot, such as push and pop for adding or removing elements.

Example of using push and pop methods to manipulate array elements.

Explanation of the clear method and three ways to clear an array in Godot.

Introduction to the duplicate method for creating copies of arrays with options for deep or shallow duplication.

Difference between deep copy and shallow copy in terms of nested arrays and dictionaries.

Demonstration of the size method to get the length of an array.

Overview of coding examples provided in the GD script file.

Invitation to download the GitHub project for hands-on experience with array manipulation.

Conclusion of the episode with a summary of array usage in Godot.

Transcripts

play00:02

hello and welcome to another episode of

play00:05

the GD script fundamental tutorial

play00:07

series in this episode we will be

play00:09

talking about arrays so what exactly is

play00:12

an array arrays are a collection of

play00:14

items stored at continues memory

play00:16

locations what that basically means is

play00:19

that when stored in memory address it's

play00:22

just done sequentially now arrays are

play00:25

used when you want to store multiple

play00:26

items together in gadot arrays can

play00:29

contain elements of different data types

play00:32

arrays and dictionaries so if you're new

play00:35

to arrays you can think of an array as

play00:37

basically a line you have someone in the

play00:39

front of the line you have someone at

play00:41

the back of the line and you have people

play00:43

in between the person at the front of

play00:46

the line is at position 0 if you're

play00:48

second in line you are at position 1 if

play00:51

you're a third position 2 and so forth

play00:53

so as you can see here this is a better

play00:55

representation of what an array actually

play00:57

looks like now a value inside an array

play01:00

is called an element and the position of

play01:03

the array is called an index position as

play01:07

you can see here the index position

play01:08

starts at 0 for our first element the

play01:12

index position of 1 for our second

play01:15

element and it continues like that

play01:17

however keep in mind that you can also

play01:20

use inverse index positions when you

play01:23

want to start at the back of your array

play01:25

in this case here if you want the last

play01:28

element of your array you'll use

play01:30

negative 1 as your index position and if

play01:33

you want the second-to-last item in your

play01:35

array you'll call that index position of

play01:38

negative 2 this is also referred to as

play01:40

an inverse index position when using

play01:43

negative numbers declaring an array in

play01:45

Godot is fairly straightforward you

play01:48

declare a variable as you would any

play01:50

other variable however when you assign

play01:52

it a value you use square brackets as

play01:54

you can see here to declare an empty

play01:57

array as our first example shows you

play01:59

just use square brackets with nothing

play02:02

inside however if you want to declare an

play02:04

array with predetermined values you'll

play02:07

simply just go ahead and type them out

play02:09

keep in mind that in Gd script your

play02:11

arrays can have different data types one

play02:15

to keep in mind is that all elements in

play02:17

your array must be separated by commas

play02:20

now let's go ahead and look at

play02:22

retrieving data from an array retrieving

play02:25

data from an array is fairly

play02:26

straightforward all you have to do is

play02:28

just type out the variable name now if

play02:31

you type out the variable name what

play02:32

you're gonna get is the entire array

play02:34

returned back however if you just want a

play02:37

specific value or rather a specific

play02:40

element at a specific index you simply

play02:43

type out the variable name followed by

play02:45

square brackets with the element

play02:47

position inside the bracket in the

play02:50

second example what you're saying is

play02:52

that we're going to get a value from the

play02:54

index position 0 and in our first

play02:57

example that would just be the value 1

play02:59

moving on

play03:02

many programming languages including GD

play03:05

script you can put a raise inside an

play03:08

array when you put in an array inside an

play03:10

array those are called sub arrays

play03:12

depending on how many layers of the

play03:14

arrays they could also be called

play03:16

two-dimensional arrays three dimensional

play03:18

arrays and so forth to create a sub

play03:21

array all you have to do is use the

play03:23

square brackets followed by another pair

play03:25

of square brackets however what gets

play03:28

tricky with sub arrays is when you want

play03:30

to get back a value as you can see here

play03:32

we are getting the first element or

play03:35

rather the element at index position 0

play03:38

which would be this right here the first

play03:41

element would be another array but if we

play03:44

want to get a specific value from that

play03:45

array we're gonna have to use a second

play03:48

pair of brackets to declare what

play03:50

position in the second array we want to

play03:53

get from in this case we want to get the

play03:55

value from the index position of 1 which

play03:58

would just be the number 2 remembering

play04:01

the elements start at an index position

play04:03

of 0 and 0 would be the integer value 1

play04:07

one cool thing about Gd script is that

play04:09

arrays come with methods you have the

play04:11

push method and the pop method you may

play04:13

find yourself using these two most of

play04:15

the time now push method adds an element

play04:18

to either the beginning or the end of an

play04:20

array the pop method removes and returns

play04:23

an elements from the beginning or end of

play04:25

an array let's go ahead and take a look

play04:27

at a quick example as you can see here

play04:29

we've declared a very basic array with

play04:32

values 1 2 3 4 and 5 in Gd script we

play04:35

have to declare where we want to push or

play04:37

where we want to pop in this case in the

play04:40

first example we are going to pop back

play04:41

which just basically means remove the

play04:44

last element of our array and return it

play04:46

in this case we have nothing to return

play04:48

it to so it just removes the last

play04:50

element as you can see here our array

play04:52

only has four values now we've removed

play04:55

the 5 now if you want to remove an

play04:58

element from the front of an array

play05:00

you'll use the pop front method so you

play05:03

can see here once we use the pop front

play05:05

method we removed the element at the

play05:08

index position of 0 now if we want to

play05:11

add elements to the beginning or end of

play05:13

the array we use the push method and

play05:15

third example you can see that we're

play05:17

going to push back and integer five

play05:19

which just basically means add five to

play05:22

the end of the array and push front to

play05:25

add an element to the beginning of a

play05:27

narang another method you may find

play05:29

yourself using is the clear method

play05:31

however in Gd script there are three

play05:34

ways to clear in array the first way is

play05:36

the very obvious declaring a new empty

play05:39

array to your existing variable in this

play05:42

case you can see here in the first

play05:44

example we are assigning an empty array

play05:46

to an already created variable named

play05:49

array the second way to clear an array

play05:52

is by using the resize method keep in

play05:54

mind that the resize method is when you

play05:57

want to basically shorten an array by

play05:59

declaring how long you'd like the length

play06:01

to be in this case what we're saying is

play06:03

we want to resize our array to have a

play06:05

length of 0 which essentially just means

play06:08

an empty array the third example shows

play06:11

the most intuitive way to clear an array

play06:13

which is just to call the clear method

play06:15

all three are viable examples of how to

play06:18

clear an array however my preferred

play06:20

method would be to use the clear method

play06:23

moving on let's look at how to duplicate

play06:25

an array to duplicate an array all you

play06:27

have to do is use the duplicate method

play06:30

what the duplicate method does is it

play06:33

essentially returns back a copy of your

play06:36

array there are two types of duplication

play06:39

in Gd script you can have deep

play06:41

duplication or shallow duplication in

play06:44

other programming languages and

play06:45

including GD script this is also

play06:47

referred to as a deep copy or a shallow

play06:50

copy to do a shallow copy in Gd script

play06:53

all you have to do is set the boolean

play06:55

value to your duplicate method to false

play06:57

just in case you don't understand the

play06:59

difference between a deep copy and a

play07:01

shallow copy a deep copy is when all

play07:04

nested arrays and dictionaries are

play07:06

duplicated and will not be shared with

play07:09

the original array a shallow copy

play07:11

however references to the original

play07:13

nested arrays and dictionaries are kept

play07:16

so that modifying a sub array or

play07:18

dictionary in the copy will also impact

play07:21

those referenced in the sorcerer what

play07:23

that essentially means is a deep copy if

play07:26

you were to change a value in a sub

play07:28

array and that

play07:29

assigned to a different variable

play07:30

changing that other variable will not

play07:33

affect your current variable however if

play07:35

you were to shallow copy affecting the

play07:38

sub-array from a different variable

play07:39

we'll change the value of your current

play07:42

variable last let's go ahead and take a

play07:45

look at getting the length of in a ram

play07:47

getting the length of an array is very

play07:48

simple all you have to do is called the

play07:51

size method and that will return an

play07:54

integer back to you you can think of a

play07:56

size method as returning the last index

play08:00

position plus one well let's go ahead

play08:03

and take a look at some coding so as you

play08:06

can see here we have a simple GD file at

play08:09

the top you can see I have declared to

play08:11

erase an empty array and a simple array

play08:14

declaring an array is fairly

play08:15

straightforward the same can be true

play08:18

with a sub array in this case you can

play08:20

see brackets or inside brackets and of

play08:23

course when we want to retrieve a value

play08:25

from a specific index we have to use

play08:28

double brackets because in our sub array

play08:31

we have two brackets one for the outer

play08:33

array and one for the inner array here I

play08:37

have an example of pushing to an array

play08:39

as you can see here we're pushing to the

play08:41

front the integer value 0 and we're

play08:43

pushing to the back and integer value 6

play08:46

if you want to pop in array you just use

play08:49

the pup front or pop back method as you

play08:52

can see here we're going to not only

play08:53

remove an element but we're going to

play08:56

return its value so as you can see here

play08:58

we have a variable we just declared and

play09:01

we're assigning it a value from the

play09:03

array that we are removing so if you

play09:06

want to remove an element and assign

play09:08

that value to a variable declaration

play09:10

using the pup method is a great way as

play09:14

you can see here we are also doing it

play09:16

for the back or rather the last element

play09:19

of the array as well now over here we

play09:21

have three different ways to clear an

play09:24

array the first way is to assign an

play09:26

existing array an empty array the second

play09:29

more intuitive way is to just call the

play09:32

clear method and the third way is to

play09:34

just resize your array to have a length

play09:37

of 0 basically the clear method is just

play09:40

the resize method at least to the Godot

play09:42

document

play09:43

at least or at least to the Godot

play09:44

documents that is now over here we are

play09:47

duplicating this is a deep copy as you

play09:50

can see we are using the duplicate

play09:52

method and we are signing the value true

play09:54

to let the compiler know that we want a

play09:57

deep copy what this means is if we were

play09:59

to take an array and push a new value

play10:02

into it as you can see here the new

play10:05

array is being pushed a value of 3

play10:08

towards the back of the array and that

play10:11

new array has been pushed to our simple

play10:15

array then deep copy doesn't change its

play10:18

value but simple array does now if you

play10:22

were to do what's called a shallow copy

play10:24

all we thought you would have to do is

play10:26

assign the duplicate method with the

play10:29

false boolean and this lets the compiler

play10:31

know that you want a shallow copy what

play10:35

that essentially means is if we have

play10:37

another array and we add a value that

play10:39

value will change the value in shallow

play10:43

copy as well as simple array now last

play10:46

but not least we have the method to get

play10:49

the length of the array to get the link

play10:52

of the array all you have to do is just

play10:54

call the size method well this was just

play10:57

a brief overview of how to use arrays in

play10:59

code please go ahead and download the

play11:02

github project that I have put a link to

play11:05

in the description area of this video

play11:07

when you download the file project go

play11:10

ahead and press the play button to see

play11:12

the values printed out to the screen so

play11:14

you can get a better understanding of

play11:16

how arrays can be manipulated well

play11:18

that's all I have for this episode I

play11:20

hope to see you in the next

Rate This

5.0 / 5 (0 votes)

Связанные теги
GDScriptArraysTutorialProgrammingData StructuresMemory LocationsIndex PositionsSub-arraysArray MethodsDeep CopyShallow Copy
Вам нужно краткое изложение на английском?