Arrays | Godot GDScript Tutorial | Ep 10
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
📚 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.
🔍 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.
🔄 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
💡Index Position
💡Element
💡Sub-array
💡Push Method
💡Pop Method
💡Clear Method
💡Duplicate Method
💡Deep Copy
💡Shallow Copy
💡Size Method
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
hello and welcome to another episode of
the GD script fundamental tutorial
series in this episode we will be
talking about arrays so what exactly is
an array arrays are a collection of
items stored at continues memory
locations what that basically means is
that when stored in memory address it's
just done sequentially now arrays are
used when you want to store multiple
items together in gadot arrays can
contain elements of different data types
arrays and dictionaries so if you're new
to arrays you can think of an array as
basically a line you have someone in the
front of the line you have someone at
the back of the line and you have people
in between the person at the front of
the line is at position 0 if you're
second in line you are at position 1 if
you're a third position 2 and so forth
so as you can see here this is a better
representation of what an array actually
looks like now a value inside an array
is called an element and the position of
the array is called an index position as
you can see here the index position
starts at 0 for our first element the
index position of 1 for our second
element and it continues like that
however keep in mind that you can also
use inverse index positions when you
want to start at the back of your array
in this case here if you want the last
element of your array you'll use
negative 1 as your index position and if
you want the second-to-last item in your
array you'll call that index position of
negative 2 this is also referred to as
an inverse index position when using
negative numbers declaring an array in
Godot is fairly straightforward you
declare a variable as you would any
other variable however when you assign
it a value you use square brackets as
you can see here to declare an empty
array as our first example shows you
just use square brackets with nothing
inside however if you want to declare an
array with predetermined values you'll
simply just go ahead and type them out
keep in mind that in Gd script your
arrays can have different data types one
to keep in mind is that all elements in
your array must be separated by commas
now let's go ahead and look at
retrieving data from an array retrieving
data from an array is fairly
straightforward all you have to do is
just type out the variable name now if
you type out the variable name what
you're gonna get is the entire array
returned back however if you just want a
specific value or rather a specific
element at a specific index you simply
type out the variable name followed by
square brackets with the element
position inside the bracket in the
second example what you're saying is
that we're going to get a value from the
index position 0 and in our first
example that would just be the value 1
moving on
many programming languages including GD
script you can put a raise inside an
array when you put in an array inside an
array those are called sub arrays
depending on how many layers of the
arrays they could also be called
two-dimensional arrays three dimensional
arrays and so forth to create a sub
array all you have to do is use the
square brackets followed by another pair
of square brackets however what gets
tricky with sub arrays is when you want
to get back a value as you can see here
we are getting the first element or
rather the element at index position 0
which would be this right here the first
element would be another array but if we
want to get a specific value from that
array we're gonna have to use a second
pair of brackets to declare what
position in the second array we want to
get from in this case we want to get the
value from the index position of 1 which
would just be the number 2 remembering
the elements start at an index position
of 0 and 0 would be the integer value 1
one cool thing about Gd script is that
arrays come with methods you have the
push method and the pop method you may
find yourself using these two most of
the time now push method adds an element
to either the beginning or the end of an
array the pop method removes and returns
an elements from the beginning or end of
an array let's go ahead and take a look
at a quick example as you can see here
we've declared a very basic array with
values 1 2 3 4 and 5 in Gd script we
have to declare where we want to push or
where we want to pop in this case in the
first example we are going to pop back
which just basically means remove the
last element of our array and return it
in this case we have nothing to return
it to so it just removes the last
element as you can see here our array
only has four values now we've removed
the 5 now if you want to remove an
element from the front of an array
you'll use the pop front method so you
can see here once we use the pop front
method we removed the element at the
index position of 0 now if we want to
add elements to the beginning or end of
the array we use the push method and
third example you can see that we're
going to push back and integer five
which just basically means add five to
the end of the array and push front to
add an element to the beginning of a
narang another method you may find
yourself using is the clear method
however in Gd script there are three
ways to clear in array the first way is
the very obvious declaring a new empty
array to your existing variable in this
case you can see here in the first
example we are assigning an empty array
to an already created variable named
array the second way to clear an array
is by using the resize method keep in
mind that the resize method is when you
want to basically shorten an array by
declaring how long you'd like the length
to be in this case what we're saying is
we want to resize our array to have a
length of 0 which essentially just means
an empty array the third example shows
the most intuitive way to clear an array
which is just to call the clear method
all three are viable examples of how to
clear an array however my preferred
method would be to use the clear method
moving on let's look at how to duplicate
an array to duplicate an array all you
have to do is use the duplicate method
what the duplicate method does is it
essentially returns back a copy of your
array there are two types of duplication
in Gd script you can have deep
duplication or shallow duplication in
other programming languages and
including GD script this is also
referred to as a deep copy or a shallow
copy to do a shallow copy in Gd script
all you have to do is set the boolean
value to your duplicate method to false
just in case you don't understand the
difference between a deep copy and a
shallow copy a deep copy is when all
nested arrays and dictionaries are
duplicated and will not be shared with
the original array a shallow copy
however references to the original
nested arrays and dictionaries are kept
so that modifying a sub array or
dictionary in the copy will also impact
those referenced in the sorcerer what
that essentially means is a deep copy if
you were to change a value in a sub
array and that
assigned to a different variable
changing that other variable will not
affect your current variable however if
you were to shallow copy affecting the
sub-array from a different variable
we'll change the value of your current
variable last let's go ahead and take a
look at getting the length of in a ram
getting the length of an array is very
simple all you have to do is called the
size method and that will return an
integer back to you you can think of a
size method as returning the last index
position plus one well let's go ahead
and take a look at some coding so as you
can see here we have a simple GD file at
the top you can see I have declared to
erase an empty array and a simple array
declaring an array is fairly
straightforward the same can be true
with a sub array in this case you can
see brackets or inside brackets and of
course when we want to retrieve a value
from a specific index we have to use
double brackets because in our sub array
we have two brackets one for the outer
array and one for the inner array here I
have an example of pushing to an array
as you can see here we're pushing to the
front the integer value 0 and we're
pushing to the back and integer value 6
if you want to pop in array you just use
the pup front or pop back method as you
can see here we're going to not only
remove an element but we're going to
return its value so as you can see here
we have a variable we just declared and
we're assigning it a value from the
array that we are removing so if you
want to remove an element and assign
that value to a variable declaration
using the pup method is a great way as
you can see here we are also doing it
for the back or rather the last element
of the array as well now over here we
have three different ways to clear an
array the first way is to assign an
existing array an empty array the second
more intuitive way is to just call the
clear method and the third way is to
just resize your array to have a length
of 0 basically the clear method is just
the resize method at least to the Godot
document
at least or at least to the Godot
documents that is now over here we are
duplicating this is a deep copy as you
can see we are using the duplicate
method and we are signing the value true
to let the compiler know that we want a
deep copy what this means is if we were
to take an array and push a new value
into it as you can see here the new
array is being pushed a value of 3
towards the back of the array and that
new array has been pushed to our simple
array then deep copy doesn't change its
value but simple array does now if you
were to do what's called a shallow copy
all we thought you would have to do is
assign the duplicate method with the
false boolean and this lets the compiler
know that you want a shallow copy what
that essentially means is if we have
another array and we add a value that
value will change the value in shallow
copy as well as simple array now last
but not least we have the method to get
the length of the array to get the link
of the array all you have to do is just
call the size method well this was just
a brief overview of how to use arrays in
code please go ahead and download the
github project that I have put a link to
in the description area of this video
when you download the file project go
ahead and press the play button to see
the values printed out to the screen so
you can get a better understanding of
how arrays can be manipulated well
that's all I have for this episode I
hope to see you in the next
5.0 / 5 (0 votes)