Improve the Dictionary class documentation

This closes https://github.com/godotengine/godot-docs/issues/3376.

(cherry picked from commit 88da90f2bf)
This commit is contained in:
Hugo Locurcio 2020-04-12 22:44:44 +02:00 committed by Rémi Verschelde
parent eab7342321
commit 6c4a0ec75a
1 changed files with 16 additions and 6 deletions

View File

@ -4,8 +4,9 @@
Dictionary type. Dictionary type.
</brief_description> </brief_description>
<description> <description>
Dictionary type. Associative container which contains values referenced by unique keys. Dictionary are composed of pairs of keys (which must be unique) and values. You can define a dictionary by placing a comma separated list of [code]key: value[/code] pairs in curly braces [code]{}[/code]. Dictionary type. Associative container which contains values referenced by unique keys. Dictionaries are composed of pairs of keys (which must be unique) and values. Dictionaries will preserve the insertion order when adding elements, even though this may not be reflected when printing the dictionary. In other programming languages, this data structure is sometimes referred to as an hash map or associative array.
Erasing elements while iterating over them [b]is not supported[/b]. You can define a dictionary by placing a comma-separated list of [code]key: value[/code] pairs in curly braces [code]{}[/code].
Erasing elements while iterating over them [b]is not supported[/b] and will result in undefined behavior.
Creating a dictionary: Creating a dictionary:
[codeblock] [codeblock]
var my_dir = {} # Creates an empty dictionary. var my_dir = {} # Creates an empty dictionary.
@ -16,15 +17,16 @@
key3: value3, key3: value3,
} }
[/codeblock] [/codeblock]
You can access values of a dictionary by referencing appropriate key in above example [code]points_dir["White"][/code] would return value of 50. You can access a dictionary's values by referencing the appropriate key. In the above example, [code]points_dir["White"][/code] will return [code]50[/code]. You can also write [code]points_dir.White[/code], which is equivalent. However, you'll have to use the bracket syntax if the key you're accessing the dictionary with isn't a fixed string (such as a number or variable).
[codeblock] [codeblock]
export(String, "White", "Yellow", "Orange") var my_color export(String, "White", "Yellow", "Orange") var my_color
var points_dir = {"White": 50, "Yellow": 75, "Orange": 100} var points_dir = {"White": 50, "Yellow": 75, "Orange": 100}
func _ready(): func _ready():
# We can't use dot syntax here as `my_color` is a variable.
var points = points_dir[my_color] var points = points_dir[my_color]
[/codeblock] [/codeblock]
In the above code [code]points[/code] will be assigned the value that is paired with the appropriate color selected in [code]my_color[/code]. In the above code, [code]points[/code] will be assigned the value that is paired with the appropriate color selected in [code]my_color[/code].
Dictionaries can contain more complex data: Dictionaries can contain more complex data:
[codeblock] [codeblock]
my_dir = {"First Array": [1, 2, 3, 4]} # Assigns an Array to a String key. my_dir = {"First Array": [1, 2, 3, 4]} # Assigns an Array to a String key.
@ -36,9 +38,17 @@
[/codeblock] [/codeblock]
Finally, dictionaries can contain different types of keys and values in the same dictionary: Finally, dictionaries can contain different types of keys and values in the same dictionary:
[codeblock] [codeblock]
var my_dir = {"String Key": 5, 4: [1, 2, 3], 7: "Hello"} # This is a valid dictionary. # This is a valid dictionary.
# To access the string "Nested value" below, use `my_dir.sub_dir.sub_key` or `my_dir["sub_dir"]["sub_key"]`.
# Indexing styles can be mixed and matched depending on your needs.
var my_dir = {
"String Key": 5,
4: [1, 2, 3],
7: "Hello",
"sub_dir": {"sub_key": "Nested value"},
}
[/codeblock] [/codeblock]
[b]Note:[/b] Unlike [Array]s you can't compare dictionaries directly: [b]Note:[/b] Unlike [Array]s, you can't compare dictionaries directly:
[codeblock] [codeblock]
array1 = [1, 2, 3] array1 = [1, 2, 3]
array2 = [1, 2, 3] array2 = [1, 2, 3]