0% found this document useful (0 votes)
65 views4 pages

Dictionaries Godot GDScript Tutorial Ep 12 Godot Tutorials

This document discusses dictionaries in Godot GDScript. Dictionaries are key-value stores that allow accessing values using keys. Keys can be strings, integers, floats or booleans. Values are accessed using dictionary[key] syntax or dot notation for string keys. New key-value pairs can be assigned or existing ones modified. Dictionaries are compared using their hash, not by value. Keys can be erased using the erase method.

Uploaded by

Chris Lewinsky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views4 pages

Dictionaries Godot GDScript Tutorial Ep 12 Godot Tutorials

This document discusses dictionaries in Godot GDScript. Dictionaries are key-value stores that allow accessing values using keys. Keys can be strings, integers, floats or booleans. Values are accessed using dictionary[key] syntax or dot notation for string keys. New key-value pairs can be assigned or existing ones modified. Dictionaries are compared using their hash, not by value. Keys can be erased using the erase method.

Uploaded by

Chris Lewinsky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Dictionaries | Godot GDScript Tutorial | Ep 12 | Godot Tutorials 5/8/23 4:41 PM

Courses / Introduction-To-Gdscript / Godot-Tutorials-Gdscript-12

Dictionaries | Godot GDScript Tutorial | Ep 12

Dictionaries | Godot GDScript Tutorial | Ep 12

PREV EPISODE NEXT EPISODE

Learning Materials
 Article  Resource

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-12 Page 1 of 4
Dictionaries | Godot GDScript Tutorial | Ep 12 | Godot Tutorials 5/8/23 4:41 PM

Dictionaries
Dictionaries, also referred to as a key-value store, are associative containers that contain values referenced by keys.

var simpleDictionary = {"name": "John"}

In the code above, the key name, while the value is "John"

You are also able to use other datatypees besides strings as keys:

var integerDictionary = {1: "John"}


var floatDictionary = {2.13: "Jane"}
var booleanDictionary = {true: "Sarah"}

Accessing Dictionaries

# Create Dictionaries
var simpleDictionary = {"name": "John"}
var integerDictionary = {1: "Jane"}
var floatDictionary = {2.13: "Josh"}
var booleanDictionary = {true: "Sarah"}

# Retrieve values
var getJohn = simpleDictionary["name"] # "John"
var getJane = integerDictionary[1] # "Jane"
var getJosh = floatDictionary[2.13] # "Josh"
var getJohn = booleanDictionary[true] # "Sarah"

# You can use dot notation if retrieving string keys


var getJohnAgain = simpleDictionary.name # "John"

Assigning Values to an Existing Key

var simpleDictionary = {"name": "John"}

# One way
simpleDictionary["name"] = "Jane"

# Another way
simpleDictionary.name = "Jane Doe"

Creating New Key-Value Pair

var simpleDictionary = {"name": "Jane"}

simpleDictionary["birthdate"] = "December 2020"

simpleDictionary.age = 0

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-12 Page 2 of 4
Dictionaries | Godot GDScript Tutorial | Ep 12 | Godot Tutorials 5/8/23 4:41 PM

Comparing Dictionaries key-value pairs


If you compare dictionaries with a comparison operator, you will return false even if the dictionaries compared are exactly the same:

var simpleDictionary = {"name": "Jane"}


var sameDictionary = {"name": "Jane"}

if(simpleDictionary == sameDictionary):
# Block of code never runs

To get the desired results when coparing dictionaries, use the hash method:

var simpleDictionary = {"name": "Jane"}


var sameDictionary = {"name": "Jane"}

if(simpleDictionary.hash() == sameDictionary.hash()):
# Block of code runs

Delete a key-value pair


To delete a key-value pair, use the erase method:

var simpleDictionary = {"name": "Jane"}

simpleDictionary.erase("name")

Creative Common License

Dictionaries | Godot GDScript Tutorial | Ep 12 video & article by Godot Tutorials is licensed under a Creative Commons Attribution-
ShareAlike 4.0 International License .

Spread the love, share this if it helped you!

 SHARE ON TWITTER  SHARE ON FACEBOOK  SHARE ON REDDIT  SHARE WITH EMAIL

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-12 Page 3 of 4
Dictionaries | Godot GDScript Tutorial | Ep 12 | Godot Tutorials 5/8/23 4:41 PM

Subscribe to my Newsletter
Join our newsletter and get news in your inbox! We hate spam too, you
won't get any from me :)

 Email... SUBSCRIBE

GODOT TUTORIALS
WEBSITE RESOURCES LEGAL OTHER

Courses Godot Privacy Policy Contact Me

Trello Cookie Policy Website Info

Github Terms & About Me


Conditions
  
Disclaimer

©2021 Godot Tutorials | Website Powered by Hugo Framework

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-12 Page 4 of 4

You might also like