|
18 | 18 | dict_variable_key: dict_variable_value, |
19 | 19 | } |
20 | 20 |
|
21 | | - var points_dict = {"White": 50, "Yellow": 75, "Orange": 100} |
| 21 | + var points_dict = { "White": 50, "Yellow": 75, "Orange": 100 } |
22 | 22 |
|
23 | 23 | # Alternative Lua-style syntax. |
24 | 24 | # Doesn't require quotes around keys, but only string constants can be used as key names. |
|
32 | 32 | var myDict = new Godot.Collections.Dictionary(); // Creates an empty dictionary. |
33 | 33 | var pointsDict = new Godot.Collections.Dictionary |
34 | 34 | { |
35 | | - {"White", 50}, |
36 | | - {"Yellow", 75}, |
37 | | - {"Orange", 100} |
| 35 | + { "White", 50 }, |
| 36 | + { "Yellow", 75 }, |
| 37 | + { "Orange", 100 }, |
38 | 38 | }; |
39 | 39 | [/csharp] |
40 | 40 | [/codeblocks] |
41 | 41 | You can access a dictionary's value by referencing its corresponding key. In the above example, [code]points_dict["White"][/code] will return [code]50[/code]. You can also write [code]points_dict.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). |
42 | 42 | [codeblocks] |
43 | 43 | [gdscript] |
44 | 44 | @export_enum("White", "Yellow", "Orange") var my_color: String |
45 | | - var points_dict = {"White": 50, "Yellow": 75, "Orange": 100} |
| 45 | + var points_dict = { "White": 50, "Yellow": 75, "Orange": 100 } |
46 | 46 | func _ready(): |
47 | 47 | # We can't use dot syntax here as `my_color` is a variable. |
48 | 48 | var points = points_dict[my_color] |
|
52 | 52 | public string MyColor { get; set; } |
53 | 53 | private Godot.Collections.Dictionary _pointsDict = new Godot.Collections.Dictionary |
54 | 54 | { |
55 | | - {"White", 50}, |
56 | | - {"Yellow", 75}, |
57 | | - {"Orange", 100} |
| 55 | + { "White", 50 }, |
| 56 | + { "Yellow", 75 }, |
| 57 | + { "Orange", 100 }, |
58 | 58 | }; |
59 | 59 |
|
60 | 60 | public override void _Ready() |
|
74 | 74 | [csharp] |
75 | 75 | var myDict = new Godot.Collections.Dictionary |
76 | 76 | { |
77 | | - {"First Array", new Godot.Collections.Array{1, 2, 3, 4}} |
| 77 | + { "First Array", new Godot.Collections.Array { 1, 2, 3, 4 } } |
78 | 78 | }; |
79 | 79 | [/csharp] |
80 | 80 | [/codeblocks] |
81 | 81 | To add a key to an existing dictionary, access it like an existing key and assign to it: |
82 | 82 | [codeblocks] |
83 | 83 | [gdscript] |
84 | | - var points_dict = {"White": 50, "Yellow": 75, "Orange": 100} |
| 84 | + var points_dict = { "White": 50, "Yellow": 75, "Orange": 100 } |
85 | 85 | points_dict["Blue"] = 150 # Add "Blue" as a key and assign 150 as its value. |
86 | 86 | [/gdscript] |
87 | 87 | [csharp] |
88 | 88 | var pointsDict = new Godot.Collections.Dictionary |
89 | 89 | { |
90 | | - {"White", 50}, |
91 | | - {"Yellow", 75}, |
92 | | - {"Orange", 100} |
| 90 | + { "White", 50 }, |
| 91 | + { "Yellow", 75 }, |
| 92 | + { "Orange", 100 }, |
93 | 93 | }; |
94 | 94 | pointsDict["Blue"] = 150; // Add "Blue" as a key and assign 150 as its value. |
95 | 95 | [/csharp] |
|
104 | 104 | "String Key": 5, |
105 | 105 | 4: [1, 2, 3], |
106 | 106 | 7: "Hello", |
107 | | - "sub_dict": {"sub_key": "Nested value"}, |
| 107 | + "sub_dict": { "sub_key": "Nested value" }, |
108 | 108 | } |
109 | 109 | [/gdscript] |
110 | 110 | [csharp] |
111 | 111 | // This is a valid dictionary. |
112 | 112 | // To access the string "Nested value" below, use `((Godot.Collections.Dictionary)myDict["sub_dict"])["sub_key"]`. |
113 | 113 | var myDict = new Godot.Collections.Dictionary { |
114 | | - {"String Key", 5}, |
115 | | - {4, new Godot.Collections.Array{1,2,3}}, |
116 | | - {7, "Hello"}, |
117 | | - {"sub_dict", new Godot.Collections.Dictionary{{"sub_key", "Nested value"}}} |
| 114 | + { "String Key", 5 }, |
| 115 | + { 4, new Godot.Collections.Array { 1, 2, 3 } }, |
| 116 | + { 7, "Hello" }, |
| 117 | + { "sub_dict", new Godot.Collections.Dictionary { { "sub_key", "Nested value" } } }, |
118 | 118 | }; |
119 | 119 | [/csharp] |
120 | 120 | [/codeblocks] |
121 | 121 | The keys of a dictionary can be iterated with the [code]for[/code] keyword: |
122 | 122 | [codeblocks] |
123 | 123 | [gdscript] |
124 | | - var groceries = {"Orange": 20, "Apple": 2, "Banana": 4} |
| 124 | + var groceries = { "Orange": 20, "Apple": 2, "Banana": 4 } |
125 | 125 | for fruit in groceries: |
126 | 126 | var amount = groceries[fruit] |
127 | 127 | [/gdscript] |
128 | 128 | [csharp] |
129 | | - var groceries = new Godot.Collections.Dictionary{{"Orange", 20}, {"Apple", 2}, {"Banana", 4}}; |
| 129 | + var groceries = new Godot.Collections.Dictionary { { "Orange", 20 }, { "Apple", 2 }, { "Banana", 4 } }; |
130 | 130 | foreach (var (fruit, amount) in groceries) |
131 | 131 | { |
132 | 132 | // `fruit` is the key, `amount` is the value. |
|
298 | 298 | [/codeblocks] |
299 | 299 | In GDScript, this is equivalent to the [code]in[/code] operator: |
300 | 300 | [codeblock] |
301 | | - if "Godot" in {"Godot": 4}: |
| 301 | + if "Godot" in { "Godot": 4 }: |
302 | 302 | print("The key is here!") # Will be printed. |
303 | 303 | [/codeblock] |
304 | 304 | [b]Note:[/b] This method returns [code]true[/code] as long as the [param key] exists, even if its corresponding value is [code]null[/code]. |
|
310 | 310 | <description> |
311 | 311 | Returns [code]true[/code] if the dictionary contains all keys in the given [param keys] array. |
312 | 312 | [codeblock] |
313 | | - var data = {"width" : 10, "height" : 20} |
| 313 | + var data = { "width": 10, "height": 20 } |
314 | 314 | data.has_all(["height", "width"]) # Returns true |
315 | 315 | [/codeblock] |
316 | 316 | </description> |
|
321 | 321 | Returns a hashed 32-bit integer value representing the dictionary contents. |
322 | 322 | [codeblocks] |
323 | 323 | [gdscript] |
324 | | - var dict1 = {"A": 10, "B": 2} |
325 | | - var dict2 = {"A": 10, "B": 2} |
| 324 | + var dict1 = { "A": 10, "B": 2 } |
| 325 | + var dict2 = { "A": 10, "B": 2 } |
326 | 326 |
|
327 | 327 | print(dict1.hash() == dict2.hash()) # Prints true |
328 | 328 | [/gdscript] |
329 | 329 | [csharp] |
330 | | - var dict1 = new Godot.Collections.Dictionary{{"A", 10}, {"B", 2}}; |
331 | | - var dict2 = new Godot.Collections.Dictionary{{"A", 10}, {"B", 2}}; |
| 330 | + var dict1 = new Godot.Collections.Dictionary { { "A", 10 }, { "B", 2 } }; |
| 331 | + var dict2 = new Godot.Collections.Dictionary { { "A", 10 }, { "B", 2 } }; |
332 | 332 |
|
333 | 333 | // Godot.Collections.Dictionary has no Hash() method. Use GD.Hash() instead. |
334 | 334 | GD.Print(GD.Hash(dict1) == GD.Hash(dict2)); // Prints True |
|
0 commit comments