Home Blog Gaming

GDScript Dictionaries

Godot Tutorials > GDScript Dictionaries

GDScript Dictionaries store data. They are similar to arrays, but are not indexed. With dictionaries you declare the name of the keys and the data that is stored in it. GDScript Dictionaries use JSON object syntax. If you are familiar with JavaScript or JSON, you can think of Dictionaries like an object.

GDScript Dictionaries Video Tutorial

Dictionary Example

Let's build a GDScript Dictionary named Florida that holds data about Florida.

var florida = {"fruit": "Orange",
 "tax": .06,
 "governor": "Republican",
 "climate": "Tropical"}

➼ Key is fruit - "Orange" is the value
➼ Key is tax - .06 is the value
➼ Key is governor - Republican is the value
➼ Key is climate - Tropical is the value

Now, let's access Florida's climate.

print (florida.climate)

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
Tropical
** Debug Process Stopped **

Another way to excess the value stored in climate:

 print (florida["climate"])

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
Tropical
** Debug Process Stopped **

Sometimes you might write code that determines the key you want to access. In this case, the second method is useful. Let's say your building an application that allows the user to add data to the florida dictionary:

# function to create new diction item
func add_data (key,data):
  florida[key] = data
  print (florida[key])

func _ready():
  # create a new dictionary item and print the data
  add_data("coledest_month", "January")

Output

** Debug Process Started **
  OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
  January
  ** Debug Process Stopped **
  

Empty Dictionary

Next, we will test to see if florida is empty.

  # Is florida empty?
  if florida.empty():
    print ("no data to report")
  else: print (florida)

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
{climate:Tropical, coledest_month:January, fruit:Orange, governor:Republican, sales tax:0.06}
** Debug Process Stopped **

If there was no data stored insde florida the output would read "no data to report", because we did put data inside florida, the code outputs florida.


Does a dictionary contain a key?

Sometimes you might want to test if a dictionary contains a particular key.

  if florida.has("fruit"):
    print (florida.fruit)

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
Orange
** Debug Process Stopped **
 

When would you use this in game development? Let's say you have a dictionary named "player" that holds a player's data. If they have varified their account an item with "verified" is added to player and the data stored in it is the date their account was verified. There may be features that only verified accounts are allow to do. You could test if verified exsits in player to make sure only verified players can execute certain tasks.

What are the keys?

Sometimes you might want a list of a dictionary's keys. You can do so using the keys() function.

  var keys = florida.keys()
  print(keys)

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
[fruit, sales tax, governor, climate, coledest_month]
** Debug Process Stopped **
   

keys() returns an array of key names.


What are the values?

Sometimes you might want a list of a dictionary's values. You can do so using the values() function.

var values = florida.values()
print(values)

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
[Orange, 0.06, Republican, Tropical, January]
** Debug Process Stopped **
  

values() returns an array of value names.


How many elements?

Sometimes you might want to know the number of elements. You can do so using the size() function.

var size = florida.size()
print(size)

Output

** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
5
** Debug Process Stopped **
  

sizes() returns an integer equal to the number of elements.

Homework

Write GDScript code to complete the following problems
  1. Declare 3 dictionaries that store book data. The book data you wish to store is: Title, Author, Year Published, Fiction/Non-fiction. You can use any books you like.
  2. Print the names of your books.
  3. Test if any of the books are named "City of Girls".
  4. Print the dictionary keys.
  5. If the book was published before 1950, a new key named "age" will be added to the dictionary. "Age" will store the age of the book.

Need Homework Help?

Donate

Linux Gaming Blog - Coding Commanders
Linux Gaming King Hatnix on Twitch
Linux (Ubuntu)+ NodeJS + Postgres
LAMP - Linux, Apache 2, MySQL, PHP
Sexy Linux T-shirt
Coding Commanders Twitter
Coding Commanders YouTube
Twitch: daisychaincosplay Live Coding!!!
Learn PHP on Linux with Commander Candy
Coding Commanders YouTube
Coding Commanders Twitter
Learn HTML on Linux with Commander Candy
Previous Lesson | Next Lesson