Dictionaries | Godot GDScript Tutorial | Ep 12

Godot Tutorials
17 Apr 202010:04

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

00:00

πŸ“š 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.

05:02

πŸ” 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

In the context of the video, a 'Dictionary' is a data structure in GDScript that stores values referenced by unique keys, akin to an associative array or a hash map in other programming languages. It is central to the video's theme as it is the main topic being discussed. For example, the script describes creating a dictionary with unique keys and values, and how to access and manipulate these values.

πŸ’‘Key-Value Store

A 'Key-Value Store' refers to a type of data storage where data is stored in pairs of keys and values. The video uses this term to describe dictionaries, emphasizing that each key in a dictionary is unique and associated with a particular value. This concept is fundamental to understanding how dictionaries work in GDScript.

πŸ’‘GDScript

GDScript is the scripting language used in the Godot game engine for creating game logic and interactions. The video is a tutorial on using dictionaries within GDScript, demonstrating how to create, access, and modify dictionaries as part of game development.

πŸ’‘Variable

In programming, a 'Variable' is a storage location paired with a name, which contains some known or unknown quantity of information referred to as a value. The video script explains that creating a dictionary in GDScript is similar to creating a variable, but with the use of curly braces to denote the dictionary structure.

πŸ’‘Key

A 'Key' in the context of dictionaries is a unique identifier used to access the associated value within the dictionary. The script explains that keys can be of various literal types, such as strings or integers, and they are used to retrieve or set values in the dictionary.

πŸ’‘Value

A 'Value' is the data stored in a dictionary that is associated with a specific key. The video provides examples of assigning values to keys, such as strings, integers, arrays, or even other dictionaries, demonstrating the flexibility of GDScript dictionaries.

πŸ’‘Dot Notation

In GDScript, 'Dot Notation' is used to access properties or methods of an object. The script mentions using dot notation to add a new key-value pair to an existing dictionary, where GDScript automatically converts the key after the dot into a string.

πŸ’‘Square Brackets

The video explains that 'Square Brackets' are used in GDScript to access values in a dictionary by key. It is noted that when using square brackets, you can assign integer or string values as keys, providing examples of both usages.

πŸ’‘Hash Method

The 'Hash Method' in GDScript is used to compare the contents of two dictionaries. The video script explains that instead of directly comparing two dictionaries, you should use their hash values to determine if they are equivalent. This is demonstrated with an example where the hash method is called on dictionary variables.

πŸ’‘Case-Sensitivity

The video script points out that when using strings as keys in a GDScript dictionary, the keys are 'Case-Sensitive', meaning that 'Key' and 'key' would be considered two different keys. This is an important detail when accessing or adding to a dictionary.

πŸ’‘Clear Method

The 'Clear Method' is used in GDScript to remove all key-value pairs from a dictionary. The video script provides an example of how to use this method to empty a dictionary, which is useful for resetting or reinitializing data structures.

πŸ’‘Erase Method

The 'Erase Method' in GDScript is used to remove a specific key-value pair from a dictionary based on the key. The video script explains how to use this method to delete a particular entry from a dictionary, showcasing its use with an example.

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

play00:01

hello and welcome to another episode in

play00:04

the GD script fundamental tutorial

play00:06

series in this episode we will be

play00:08

talking about dictionaries so what

play00:10

exactly is a dictionary a dictionary or

play00:12

associative containers that contains

play00:16

values reference by unique keys these

play00:18

are also called a key value store let's

play00:21

go ahead and look at how you would go on

play00:23

to creating a dictionary in Gd script

play00:26

creating a dictionary it's like creating

play00:28

a variable first you use the variable

play00:31

keyword var followed by a name for your

play00:33

variable and followed by the equal sign

play00:35

operator the most important thing that

play00:37

separates a regular variable rather

play00:40

assigning a data type versus assigning a

play00:42

dictionary to your variable will be the

play00:44

curly braces now when assigning a

play00:46

dictionary you need something called a

play00:48

key and a value your key is what you

play00:51

type out first and your key can be any

play00:54

literal value the most common literal

play00:57

values to use as a key would be the

play00:59

string value however you can use

play01:00

integers and boolean x' after your key

play01:03

it is followed by the colon symbol

play01:05

followed by a value you want to assign

play01:07

to your key so let's go ahead and see

play01:10

some examples of declaring dictionaries

play01:12

in Godot the first example is an empty

play01:15

dictionary to declare an empty

play01:16

dictionary just type out your curly

play01:19

braces with nothing inside thus an empty

play01:21

dictionary and the second example we

play01:23

have a basic key value pair we've

play01:25

created the key and double quotations

play01:28

our key name rather our key is called

play01:30

name followed by string value John as

play01:33

you can see in the third example we can

play01:36

also use integers for our keys however

play01:38

the format is the same your key is

play01:40

followed by a colon followed by a value

play01:42

lastly you can use complex data inside

play01:46

your dictionaries in this example we

play01:48

have a key called 1 followed by an array

play01:51

value being assigned to our King one

play01:54

cool thing is you can even add other

play01:56

dictionaries inside your dictionary now

play01:58

to access a value in your dictionary all

play02:02

you have to do is type out your variable

play02:05

name that's a dictionary followed by

play02:07

brackets followed by the name of your

play02:11

key now if your keys using double

play02:13

quotations

play02:14

rather if you're using a string as your

play02:16

key you're gonna have to use the double

play02:18

quotations however if you're using an

play02:20

integer value all you have to do is type

play02:23

out the integer value in your square

play02:24

bracket in the first example by typing

play02:27

out dictionary container square brackets

play02:29

double quotations name we get the value

play02:32

associated to that key in this case we

play02:34

get the value we get the string value

play02:36

John returned back in the second example

play02:39

by using the key integer value 1 we

play02:42

return back the string value John again

play02:44

accessing dictionaries are fairly

play02:46

straightforward if you're using a string

play02:48

as your key you've got to put your

play02:49

string in the square brackets if you're

play02:51

using an integer value as your key you

play02:54

got to put your integer value in the

play02:55

square brackets keep in mind when using

play02:57

strings as keys they are case-sensitive

play02:59

now to add a key and value pair to an

play03:03

existing dictionary in Gd script all you

play03:05

have to do is type out your variable

play03:08

name followed by the dot notation or

play03:10

period followed by a new name for your

play03:13

key followed by an equal sign followed

play03:15

by the value you want to pair to that

play03:18

key notice how we don't have double

play03:20

quotations when using the dot notation

play03:22

this is because Gd script will

play03:24

automatically turn everything after the

play03:27

period as a string for your King however

play03:29

if you want a little more control over

play03:32

your key name such as if you want to use

play03:36

an integer value and all you have to do

play03:38

is instead of using the dot notation you

play03:40

use the square brackets by using the

play03:42

brackets you can assign integer values

play03:44

as your key or string values in the

play03:47

second example we have a new key a

play03:49

rather a string value called new key or

play03:51

as you can see in the third example

play03:53

we're using an integer value as our key

play03:55

and assigning at the integer value 100

play03:57

to it one thing to keep in mind with

play04:00

your dictionaries is that you cannot do

play04:02

direct comparisons with the double equal

play04:04

signs for example let's take a look at

play04:06

this line of code dictionary one

play04:08

equivalent to dictionary two fewer to do

play04:10

this the value returned to you would be

play04:12

false all the time even if the key value

play04:15

pairs are the same in both dictionaries

play04:17

with dictionaries you have to do

play04:19

something special in order to get the

play04:22

comparison operator to work and that's

play04:24

very simple all you have to do is use

play04:26

the hash method

play04:27

so instead of directly comparing them

play04:29

through their variable names all you

play04:31

have to do is call their variable name

play04:33

followed by the dot notation followed by

play04:35

the hash method this will compare both

play04:38

dictionaries and if the hashes are the

play04:41

same it will return back true however if

play04:43

they are not the same the value returned

play04:45

back will be false let's go ahead and

play04:47

take a look at how you can go ahead and

play04:49

clear a dictionary to clear an entire

play04:52

dictionary all you have to do is use the

play04:54

clear method to remove a specific key

play04:57

from your dictionary all you have to do

play04:59

is use the erase method to use the erase

play05:02

method all you have to do is type out

play05:04

the word

play05:04

or rather the key word erase followed by

play05:07

the literal value you're using for your

play05:10

key now let's go ahead and look at some

play05:12

code so as you can see here we have a

play05:14

basic GD file and the first example here

play05:17

we have an empty dictionary fairly

play05:19

straightforward in our second example

play05:21

we're creating a dictionary and as you

play05:23

can see here our keys are the integer

play05:26

value 1 and the string value King so

play05:28

again just something to note you can use

play05:30

integer values and string values as your

play05:32

keys and notice that the values being

play05:34

paired to each key is a string value

play05:36

however you can get even more

play05:38

complicated than that you can add arrays

play05:40

or even dictionaries which we'll be

play05:42

seeing a little later now to add a key

play05:45

to an existing dictionary you can do

play05:47

that in two ways either through the

play05:49

square brackets followed by the literal

play05:52

value you want as your key or you can

play05:54

use the dot notation which GT script

play05:56

will automatically convert into a string

play05:58

value for your cue and as you can see

play06:00

here we're using square brackets to

play06:02

assign an integer value as our key

play06:04

followed by assigning an array value to

play06:08

that key to get a value from a key all

play06:10

you have to do is call your variable

play06:12

name followed by square brackets

play06:13

followed by the literal value you're

play06:16

using as your key in this case we can

play06:18

grab it through an integer value or a

play06:20

string value as long as the key exists

play06:23

in our dictionary now in this example

play06:26

here I want to show you how to assign a

play06:28

dictionary to another dictionary and how

play06:30

to retrieve data from that dictionary so

play06:32

as you can see here we've created two

play06:34

dictionaries dictionary one in

play06:36

dictionary two and we're assigning it a

play06:39

key value pair that key being a string

play06:41

value

play06:41

key and the value is an array very basic

play06:44

very simple to do now to assign a

play06:46

dictionary to a dictionary will look

play06:49

something like this

play06:50

as you can see here we have a key and we

play06:53

use the variable of our dictionary as

play06:56

the value in this case we're using the

play06:59

dictionary to we've just declared in the

play07:02

line above this code now just like an

play07:05

array to grab the value it's exactly

play07:08

like you would with a nested array now

play07:11

as you can see here we have oops that is

play07:14

room let me change that to dictionary

play07:16

thrinng so you can see here when we call

play07:18

our dictionary value we first want to

play07:21

grab the key in our first layer which is

play07:23

named key and then we use square

play07:25

brackets again to grab the key from our

play07:28

second layer in this case we're grabbing

play07:30

key if I were to name this key one then

play07:35

it would look something like this so

play07:36

again we require a variable name

play07:39

followed by square brackets followed by

play07:41

the key and our first layer in the

play07:43

dictionary as you can see here key and

play07:46

key and then we do square brackets again

play07:49

to grab the key from our second layer

play07:52

which is key one two key one in this

play07:54

case what this right here returns is

play07:57

going to be the array of value we've

play07:59

assigned to our second layer dictionary

play08:01

let me go ahead and reset this in this

play08:03

case when we print it out we're going to

play08:05

get the array value here last I want to

play08:08

show you how to compare your

play08:10

dictionaries keep in mind if you were to

play08:12

do a direct comparison like you would

play08:14

with variables you will get false return

play08:17

back and that's not what you want in

play08:19

this case Dictionary one and dictionary

play08:21

two when compared against each other

play08:23

through the double equal sign will

play08:26

return false and as you can see here

play08:29

dictionary one in Dictionary two are the

play08:32

exact same dictionary key value pair so

play08:36

it doesn't make sense for this to happen

play08:38

instead what you're going to want to do

play08:40

is you're going to want to compare the

play08:42

dictionary hashes against each other to

play08:45

convert a dictionary into its hash

play08:46

format all you have to do is use the

play08:48

hash method so in this case we have

play08:50

dictionary one dot notation hash method

play08:53

and we're comparing that to

play08:55

Neri - Wow it's using its hash method

play08:57

when you do this what we're gonna get

play08:59

back is true for any of you who are

play09:01

curious about the hash method all of the

play09:03

hash method does is simply converts your

play09:06

dictionary into a set of integers and

play09:08

then returns that value back and then

play09:11

you're comparing those values against

play09:13

each other last example let's go ahead

play09:15

and take a look at how to clear and

play09:17

remove a specific key from your

play09:18

dictionary to remove all key value pairs

play09:22

in your dictionary all you have to do is

play09:24

use the clear method

play09:26

don't forget the parentheses and to

play09:29

remove a specific key from your

play09:31

dictionary all you have to do is use the

play09:33

method erase followed by the literal

play09:35

value you're using for your key well

play09:37

that's all I have for you in this

play09:39

episode so I went ahead and I've

play09:41

uploaded this file to github so don't

play09:44

forget to go ahead and download that

play09:46

file play around with dictionaries well

play09:48

I hope you've learned enough to start

play09:50

using dictionaries in GD script thank

play09:53

you for joining me in this episode I

play09:54

hope to see you in the next

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
GDScriptDictionariesKey-ValueData StructuresProgrammingGame DevelopmentTutorialCoding TipsScriptingGodot Engine