Dictionaries | Godot GDScript Tutorial | Ep 12
Summary
TLDRThis episode of the GD Script Fundamentals tutorial series delves into the concept and usage of dictionaries in Godot's GD Script. Dictionaries are associative containers that store values referenced by unique keys, often used as key-value stores. The tutorial explains how to create, access, and manipulate dictionaries, including adding and removing key-value pairs. It also covers how to compare dictionaries using the hash method and clear them using specific methods. The script provides examples of declaring dictionaries with various data types as keys and values, and demonstrates how to work with nested dictionaries. The episode concludes with a GitHub link for further exploration and practice.
Takeaways
- π A dictionary in GDScript is an associative container that stores values referenced by unique keys, also known as a key-value store.
- π Creating a dictionary involves using the 'var' keyword, a variable name, and curly braces with key-value pairs separated by colons.
- π― Keys can be of any literal value, with strings being the most common, but integers and booleans can also be used.
- π To declare an empty dictionary, simply use curly braces with nothing inside.
- π Accessing a value in a dictionary is done by using the variable name followed by square brackets containing the key.
- π Adding a key-value pair to an existing dictionary can be done using dot notation or square brackets for keys.
- π Complex data types, such as arrays or even other dictionaries, can be used as values within a dictionary.
- β οΈ When using strings as keys, it's important to note that they are case-sensitive.
- π To compare two dictionaries, use the 'hash' method instead of direct comparison with '==' as it converts the dictionary into a set of integers for comparison.
- ποΈ The 'clear' method can be used to remove all key-value pairs from a dictionary, while the 'erase' method is used to remove a specific key.
- π The tutorial provides an example GD file demonstrating the creation, assignment, and manipulation of dictionaries, including nested dictionaries and array values.
Q & A
What is a dictionary in the context of GDScript?
-A dictionary in GDScript is an associative container that stores values referenced by unique keys, also known as a key-value store.
How do you create a dictionary in GDScript?
-You create a dictionary in GDScript by using the 'var' keyword, followed by a variable name and an equal sign, then enclosing key-value pairs within curly braces.
What can be used as keys in a GDScript dictionary?
-Keys in a GDScript dictionary can be any literal value, with string values being the most common, but integers and booleans can also be used.
How do you declare an empty dictionary in GDScript?
-You declare an empty dictionary in GDScript by simply using curly braces with nothing inside: {}.
Can you use complex data types as values in a GDScript dictionary?
-Yes, you can use complex data types such as arrays or even other dictionaries as values in a GDScript dictionary.
How do you access a value in a dictionary using its key?
-To access a value, you type out the variable name of the dictionary followed by square brackets containing the key, using double quotes for string keys and the integer itself for integer keys.
What is the difference between using dot notation and square brackets when adding a key-value pair to an existing dictionary in GDScript?
-Dot notation automatically converts the key to a string, while square brackets allow you to use integer or string values as keys with more control.
Why can't you use direct comparison with the double equal sign for dictionaries in GDScript?
-Direct comparison with the double equal sign for dictionaries will always return false, even if the key-value pairs are the same, because you need to compare the hashes of the dictionaries instead.
How do you compare two dictionaries for equality in GDScript?
-To compare two dictionaries for equality, you should use the 'hash' method on both dictionaries and compare the resulting hash values.
What methods can be used to clear or remove a key from a dictionary in GDScript?
-The 'clear' method can be used to remove all key-value pairs from a dictionary, and the 'erase' method can be used to remove a specific key by specifying its value.
Can you assign one dictionary to another in GDScript?
-Yes, you can assign one dictionary to another by using the variable of one dictionary as the value for a key in the other dictionary.
Outlines
π Introduction to Dictionaries in GDScript
This paragraph introduces the concept of dictionaries in GDScript, explaining them as associative containers that store values referenced by unique keys, also known as key-value stores. The tutorial demonstrates how to create a dictionary using the 'var' keyword, followed by a name and curly braces. It also discusses the importance of keys and values, with examples including strings, integers, and booleans as keys, and how to assign values to these keys. The paragraph further illustrates how to declare an empty dictionary, add key-value pairs, and use complex data types like arrays and other dictionaries as values. It also covers accessing values using keys, and the case-sensitivity of string keys.
π Advanced Dictionary Operations in GDScript
The second paragraph delves into more advanced operations with dictionaries in GDScript. It explains how to add key-value pairs to an existing dictionary using both square brackets and dot notation, emphasizing the automatic conversion of keys to strings when using dot notation. The paragraph also covers accessing values from nested dictionaries and the process of assigning one dictionary as a value to a key in another dictionary. It discusses the limitations of direct dictionary comparison and introduces the 'hash' method as a solution for comparing dictionaries. Finally, it outlines methods for clearing a dictionary entirely or removing a specific key-value pair using the 'clear' and 'erase' methods, respectively, and encourages viewers to download the provided GitHub file to practice with dictionaries.
Mindmap
Keywords
π‘Dictionary
π‘Key-Value Store
π‘GDScript
π‘Variable
π‘Key
π‘Value
π‘Dot Notation
π‘Square Brackets
π‘Hash Method
π‘Case-Sensitivity
π‘Clear Method
π‘Erase Method
Highlights
Introduction to dictionaries in GDScript as associative containers with unique keys.
Creating a dictionary in GDScript involves using variable declaration with curly braces.
Keys in dictionaries can be of any literal value, with strings being the most common.
Values in a dictionary are assigned after a colon following the key.
Demonstration of declaring empty and basic key-value pair dictionaries in Godot.
Using integers as keys in dictionaries with the same format as string keys.
Complex data types like arrays can be used as values within dictionaries.
Nesting dictionaries within dictionaries for more complex data structures.
Accessing values in a dictionary using the variable name and key in square brackets.
Case-sensitivity of string keys when accessing dictionary values.
Adding key-value pairs to an existing dictionary using dot notation or square brackets.
Direct comparison of dictionaries using the hash method for equality checks.
Clearing a dictionary using the clear method to remove all key-value pairs.
Removing a specific key from a dictionary using the erase method.
Practical examples of creating and manipulating dictionaries in GDScript files.
Assigning one dictionary to another and retrieving data from nested dictionaries.
Comparing dictionary hashes for accurate dictionary comparison.
GitHub resource provided for further learning and experimentation with dictionaries.
Transcripts
hello and welcome to another episode in
the GD script fundamental tutorial
series in this episode we will be
talking about dictionaries so what
exactly is a dictionary a dictionary or
associative containers that contains
values reference by unique keys these
are also called a key value store let's
go ahead and look at how you would go on
to creating a dictionary in Gd script
creating a dictionary it's like creating
a variable first you use the variable
keyword var followed by a name for your
variable and followed by the equal sign
operator the most important thing that
separates a regular variable rather
assigning a data type versus assigning a
dictionary to your variable will be the
curly braces now when assigning a
dictionary you need something called a
key and a value your key is what you
type out first and your key can be any
literal value the most common literal
values to use as a key would be the
string value however you can use
integers and boolean x' after your key
it is followed by the colon symbol
followed by a value you want to assign
to your key so let's go ahead and see
some examples of declaring dictionaries
in Godot the first example is an empty
dictionary to declare an empty
dictionary just type out your curly
braces with nothing inside thus an empty
dictionary and the second example we
have a basic key value pair we've
created the key and double quotations
our key name rather our key is called
name followed by string value John as
you can see in the third example we can
also use integers for our keys however
the format is the same your key is
followed by a colon followed by a value
lastly you can use complex data inside
your dictionaries in this example we
have a key called 1 followed by an array
value being assigned to our King one
cool thing is you can even add other
dictionaries inside your dictionary now
to access a value in your dictionary all
you have to do is type out your variable
name that's a dictionary followed by
brackets followed by the name of your
key now if your keys using double
quotations
rather if you're using a string as your
key you're gonna have to use the double
quotations however if you're using an
integer value all you have to do is type
out the integer value in your square
bracket in the first example by typing
out dictionary container square brackets
double quotations name we get the value
associated to that key in this case we
get the value we get the string value
John returned back in the second example
by using the key integer value 1 we
return back the string value John again
accessing dictionaries are fairly
straightforward if you're using a string
as your key you've got to put your
string in the square brackets if you're
using an integer value as your key you
got to put your integer value in the
square brackets keep in mind when using
strings as keys they are case-sensitive
now to add a key and value pair to an
existing dictionary in Gd script all you
have to do is type out your variable
name followed by the dot notation or
period followed by a new name for your
key followed by an equal sign followed
by the value you want to pair to that
key notice how we don't have double
quotations when using the dot notation
this is because Gd script will
automatically turn everything after the
period as a string for your King however
if you want a little more control over
your key name such as if you want to use
an integer value and all you have to do
is instead of using the dot notation you
use the square brackets by using the
brackets you can assign integer values
as your key or string values in the
second example we have a new key a
rather a string value called new key or
as you can see in the third example
we're using an integer value as our key
and assigning at the integer value 100
to it one thing to keep in mind with
your dictionaries is that you cannot do
direct comparisons with the double equal
signs for example let's take a look at
this line of code dictionary one
equivalent to dictionary two fewer to do
this the value returned to you would be
false all the time even if the key value
pairs are the same in both dictionaries
with dictionaries you have to do
something special in order to get the
comparison operator to work and that's
very simple all you have to do is use
the hash method
so instead of directly comparing them
through their variable names all you
have to do is call their variable name
followed by the dot notation followed by
the hash method this will compare both
dictionaries and if the hashes are the
same it will return back true however if
they are not the same the value returned
back will be false let's go ahead and
take a look at how you can go ahead and
clear a dictionary to clear an entire
dictionary all you have to do is use the
clear method to remove a specific key
from your dictionary all you have to do
is use the erase method to use the erase
method all you have to do is type out
the word
or rather the key word erase followed by
the literal value you're using for your
key now let's go ahead and look at some
code so as you can see here we have a
basic GD file and the first example here
we have an empty dictionary fairly
straightforward in our second example
we're creating a dictionary and as you
can see here our keys are the integer
value 1 and the string value King so
again just something to note you can use
integer values and string values as your
keys and notice that the values being
paired to each key is a string value
however you can get even more
complicated than that you can add arrays
or even dictionaries which we'll be
seeing a little later now to add a key
to an existing dictionary you can do
that in two ways either through the
square brackets followed by the literal
value you want as your key or you can
use the dot notation which GT script
will automatically convert into a string
value for your cue and as you can see
here we're using square brackets to
assign an integer value as our key
followed by assigning an array value to
that key to get a value from a key all
you have to do is call your variable
name followed by square brackets
followed by the literal value you're
using as your key in this case we can
grab it through an integer value or a
string value as long as the key exists
in our dictionary now in this example
here I want to show you how to assign a
dictionary to another dictionary and how
to retrieve data from that dictionary so
as you can see here we've created two
dictionaries dictionary one in
dictionary two and we're assigning it a
key value pair that key being a string
value
key and the value is an array very basic
very simple to do now to assign a
dictionary to a dictionary will look
something like this
as you can see here we have a key and we
use the variable of our dictionary as
the value in this case we're using the
dictionary to we've just declared in the
line above this code now just like an
array to grab the value it's exactly
like you would with a nested array now
as you can see here we have oops that is
room let me change that to dictionary
thrinng so you can see here when we call
our dictionary value we first want to
grab the key in our first layer which is
named key and then we use square
brackets again to grab the key from our
second layer in this case we're grabbing
key if I were to name this key one then
it would look something like this so
again we require a variable name
followed by square brackets followed by
the key and our first layer in the
dictionary as you can see here key and
key and then we do square brackets again
to grab the key from our second layer
which is key one two key one in this
case what this right here returns is
going to be the array of value we've
assigned to our second layer dictionary
let me go ahead and reset this in this
case when we print it out we're going to
get the array value here last I want to
show you how to compare your
dictionaries keep in mind if you were to
do a direct comparison like you would
with variables you will get false return
back and that's not what you want in
this case Dictionary one and dictionary
two when compared against each other
through the double equal sign will
return false and as you can see here
dictionary one in Dictionary two are the
exact same dictionary key value pair so
it doesn't make sense for this to happen
instead what you're going to want to do
is you're going to want to compare the
dictionary hashes against each other to
convert a dictionary into its hash
format all you have to do is use the
hash method so in this case we have
dictionary one dot notation hash method
and we're comparing that to
Neri - Wow it's using its hash method
when you do this what we're gonna get
back is true for any of you who are
curious about the hash method all of the
hash method does is simply converts your
dictionary into a set of integers and
then returns that value back and then
you're comparing those values against
each other last example let's go ahead
and take a look at how to clear and
remove a specific key from your
dictionary to remove all key value pairs
in your dictionary all you have to do is
use the clear method
don't forget the parentheses and to
remove a specific key from your
dictionary all you have to do is use the
method erase followed by the literal
value you're using for your key well
that's all I have for you in this
episode so I went ahead and I've
uploaded this file to github so don't
forget to go ahead and download that
file play around with dictionaries well
I hope you've learned enough to start
using dictionaries in GD script thank
you for joining me in this episode I
hope to see you in the next
5.0 / 5 (0 votes)