From 648bf6c7029a89002d1278db0f3f0153c87990eb 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 | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/doc/classes/Dictionary.xml b/doc/classes/Dictionary.xml index 5c9b22fe4a1c..85ae87e844db 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,20 @@ 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 identifiers (such as `class` or `while`). + # 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 can't use both dictionary syntaxes in a single dictionary declaration. + # Here, `some_key` is a string literal, not a variable! 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]