Skip to content

Commit

Permalink
Clarify GDScript dictionary syntax restrictions
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Calinou committed Nov 16, 2024
1 parent 98ddec4 commit 648bf6c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions doc/classes/Dictionary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</brief_description>
<description>
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]
Expand All @@ -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]
Expand Down

0 comments on commit 648bf6c

Please sign in to comment.