From ecd608e3d9e9967bc79121cb78e8147f7e4cdb63 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Fri, 15 Nov 2024 15:28:19 +0100 Subject: [PATCH] Clarify GDScript dictionary syntax restrictions - Document keys being quotable in Lua-style dictionary syntax, which can be used to bypass its limitations. - Mention duplicate dictionary keys being forbidden in declarations. - Mention that dictionary styles can't be mixed in a single declaration. --- doc/classes/Dictionary.xml | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/doc/classes/Dictionary.xml b/doc/classes/Dictionary.xml index 5c9b22fe4a1c..1d724b948a97 100644 --- a/doc/classes/Dictionary.xml +++ b/doc/classes/Dictionary.xml @@ -5,7 +5,7 @@ Dictionaries are associative containers that contain values referenced by unique keys. Dictionaries will preserve the insertion order when adding new entries. In other programming languages, this data structure is often referred to as a hash map or an associative array. - You can define a dictionary by placing a comma-separated list of [code]key: value[/code] pairs inside curly braces [code]{}[/code]. + You can define a dictionary by placing a comma-separated list of [code]key: value[/code] pairs inside curly braces [code]{}[/code]. Duplicate dictionary keys are not allowed during declaration. Creating a dictionary: [codeblocks] [gdscript] @@ -21,11 +21,19 @@ var points_dict = {"White": 50, "Yellow": 75, "Orange": 100} # Alternative Lua-style syntax. + # # Doesn't require quotes around keys, but only string constants can be used as key names. - # Additionally, key names must start with a letter or an underscore. + # Additionally, for key names to be usable without quotes, they must start with a letter + # or an underscore and must not be reserved identifers (such as `class` or `while`). # Here, `some_key` is a string literal, not a variable! + # It is still possible to use reserved identifiers, or key names that start with a digit, + # but they must be quoted. + # + # Note that while you can mix dictionary syntaxes in a single file, you cannot use both dictionary syntaxes in a single dictionary declaration. another_dict = { some_key = 42, + "class" = "wizard", # The key name must be quoted here. + "2test" = 100, # The key name must be quoted here. } [/gdscript] [csharp]