Dictionaries contain a list of key-value pairs {"key" : "value"}. The key-value pairs can be of different types:
var inventory {
"coin" : 5,
"gem" : 2,
"color" : "green"
}
Returns:
inventory["coin"] will return 5
inventory.coin will return 5
inventory.keys() will return [coin, gem, color]
inventory.keys()[0] will return coin
inventory.values() will return [5, 2, green]
inventory.values()[0] will return 5
inventory will return {coin: 5, gem: 2, color: green}
Assignments:
inventory["coin"] = 2 will change the value of coin from 5 to 2
inventory[1] = 2 is the same as above
inventory.coin = 2 is the same as above
Math:
inventory["coin"] += 2 will increment the value of coin by 2
inventory["coin"] -= 2 will decrease the value of coin by 2
Creating:
inventory["potion"] = 3 will create the key potion with the value 3
inventory.potion = 3 is the same as above
print(inventory)
print(JSON.print(inventory, "\t"))
for key in inventory:
print("%s : %d" % [key, inventory[key]])
randomize()
var a = randi() % inventory.size()
print(inventory.keys()[a])
var copied_dictionary = str2var(var2str(original_dictionary))
var inventory {
0 : ["Silver", 23],
1 : ["Gold", 56],
2 : ["Ruby", 8]
}
func _ready():
## loop through the dictionary keys to match the value to be "Gold" via the first element of the array
for key in inventory:
if inventory[key][0] == "Gold":
print ("There is Gold!!!!")
## other interesting stuff
var i = 1
print(inventory.keys().[i] ## will print 1
print(inventory[0][0]) ## will print "Silver"
print(inventory[0][1]) ## will print 23
print(inventory[2][0]) ## will print "Ruby"
print(inventory[2][1]) ## will print 8
## add a new entry to the dictionary
inventory[3] = ["Emerald", 3]
## print a random array element of the dictionary
randomize()
var rand = randi() % inventory.size()
print("%s : %s" % [inventory[rand][0], inventory[rand][1]])
## print the full inventory
for key in inventory:
print("%s: %s" % [inventory[key][0],inventory[key][1]])
You can manage a dynamic dictionary by automatically allocating a new key or updating the value of a key:
var inventory = {}
func add_item(item):
if inventory.has(item):
var temp = inventory[item]
temp += 1
inventory[item] = temp
print("You have %d %s" % [inventory[item], item])
else:
inventory[item] = 1
print("You have %d %s" % [inventory[item], item])
With the above setup, you can create a item management system. The example below shows how to decouple the responsibilities when the player picks up a coin and add it to the Player's inventory. The player enters an item's Area2D collision shape, whose body_entered signal is listened from Item.gd. Player scene is Autoloaded (global singleton), so the Player's add_item() function above can be accessed globally from any script:
extends Area2D
func _ready():
pickup(get_parent().get_name())
func pickup(item):
connect("body_entered",self,"_on_body_entered",[item])
func _on_body_entered(body,item):
if body.get_name() == "Player":
Player.add_item(item)
queue_free()
Example file: https://drive.google.com/file/d/1EngStPfZxbTGDxjVKbe7Zql3kBepNaq3/view
JSON file is downloaded to Godot project at location res://Data/Creatures.json
A Global singleton script in created:
extends Node
var creature_data: Dictionary = {}
func open_creatures_file() -> Dictionary:
var creature_data_file = File.new()
creature_data_file.open("res://Data/creatures.json", File.READ)
var creature_data_json = JSON.parse(creature_data_file.get_as_text())
creature_data_file.close()
return creature_data_json.result
func _ready():
creature_data = open_creatures_file()
# Testing
print (creature_data.keys())
var creature_type = "Goblin"
print ("%s stats are:" % [creature_type])
for key in creature_data[creature_type]:
print ("%s : %s" % [key, creature_data[creature_type][key]])
print("%s strength is %d" % [creature_type, creature_data[creature_type]["Strength"]])
The above code will print:
[Player, Human, Orc, Goblin, AdivĂa, Agoiru]
Goblin stats are:
strength : 5
intelligence : 5
dexterity : 7
endurance : 5
health : 10
Goblin strength is 5