Skip to reading
GDScript Programming & Godot

12. Dictionaries and defaults

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
Code example
func run() -> Array:
    var facts: Dictionary = {"unlocked": false, "attempts": 0}
    facts["attempts"] += 1
    facts["unlocked"] = true
    var missing: Variant = facts.get("owner", "nobody")
    return [facts["attempts"], missing, facts.has("unlocked")]

A Dictionary stores values by key. Use get(key, fallback) when a key can be missing. has checks whether the key exists, even if its value is false or null. keys and values return arrays. erase removes a key/value pair.

A dictionary of action settings must keep the exact field spelling. characterId is not character_id. Native GDScript identifiers are case-sensitive, and dictionary keys are data; a plausible spelling does not automatically become a supported setting.

Typed dictionaries are available in current Godot 4 releases, but ordinary Dictionary is the clearest boundary for PCA’s JSON-style records and cross-language calls. Keep transferred values simple and document the expected keys.

Page 20 of 378Permalink
Go to page