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.
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)
** 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"])
** 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")
** Debug Process Started ** OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2) January ** Debug Process Stopped **
Next, we will test to see if
# Is florida empty?
if florida.empty():
print ("no data to report")
else: print (florida)
** 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.
Sometimes you might want to test if a dictionary contains a particular key.
if florida.has("fruit"):
print (florida.fruit)
** 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.
Sometimes you might want a list of a dictionary's keys. You can do so using the
var keys = florida.keys()
print(keys)
** 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 **
Sometimes you might want a list of a dictionary's values. You can do so using the
var values = florida.values()
print(values)
** 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 **
Sometimes you might want to know the number of elements. You can do so using the
var size = florida.size()
print(size)
** Debug Process Started ** OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2) 5 ** Debug Process Stopped **