Merge pull request #107357 from ProgrammerOnCoffee/fix-dictionary-style

Enforce GDScript and C# dictionary spacing style guidelines in code samples
This commit is contained in:
Thaddeus Crews 2025-06-10 19:22:43 -05:00
commit 1bbfe637c6
No known key found for this signature in database
GPG Key ID: 8C6E5FEB5FC03CCC
6 changed files with 39 additions and 39 deletions

View File

@ -53,7 +53,7 @@
[/codeblock] [/codeblock]
[b]Note:[/b] [Dictionary] does not support the above due to ambiguity with keys. [b]Note:[/b] [Dictionary] does not support the above due to ambiguity with keys.
[codeblock] [codeblock]
var dictionary = {"hello": "world"} var dictionary = { "hello": "world" }
# This will not work, `clear` is treated as a key. # This will not work, `clear` is treated as a key.
tween.tween_callback(dictionary.clear) tween.tween_callback(dictionary.clear)

View File

@ -18,7 +18,7 @@
dict_variable_key: dict_variable_value, dict_variable_key: dict_variable_value,
} }
var points_dict = {"White": 50, "Yellow": 75, "Orange": 100} var points_dict = { "White": 50, "Yellow": 75, "Orange": 100 }
# Alternative Lua-style syntax. # Alternative Lua-style syntax.
# Doesn't require quotes around keys, but only string constants can be used as key names. # Doesn't require quotes around keys, but only string constants can be used as key names.
@ -32,9 +32,9 @@
var myDict = new Godot.Collections.Dictionary(); // Creates an empty dictionary. var myDict = new Godot.Collections.Dictionary(); // Creates an empty dictionary.
var pointsDict = new Godot.Collections.Dictionary var pointsDict = new Godot.Collections.Dictionary
{ {
{"White", 50}, { "White", 50 },
{"Yellow", 75}, { "Yellow", 75 },
{"Orange", 100} { "Orange", 100 },
}; };
[/csharp] [/csharp]
[/codeblocks] [/codeblocks]
@ -42,7 +42,7 @@
[codeblocks] [codeblocks]
[gdscript] [gdscript]
@export_enum("White", "Yellow", "Orange") var my_color: String @export_enum("White", "Yellow", "Orange") var my_color: String
var points_dict = {"White": 50, "Yellow": 75, "Orange": 100} var points_dict = { "White": 50, "Yellow": 75, "Orange": 100 }
func _ready(): func _ready():
# We can't use dot syntax here as `my_color` is a variable. # We can't use dot syntax here as `my_color` is a variable.
var points = points_dict[my_color] var points = points_dict[my_color]
@ -52,9 +52,9 @@
public string MyColor { get; set; } public string MyColor { get; set; }
private Godot.Collections.Dictionary _pointsDict = new Godot.Collections.Dictionary private Godot.Collections.Dictionary _pointsDict = new Godot.Collections.Dictionary
{ {
{"White", 50}, { "White", 50 },
{"Yellow", 75}, { "Yellow", 75 },
{"Orange", 100} { "Orange", 100 },
}; };
public override void _Ready() public override void _Ready()
@ -74,22 +74,22 @@
[csharp] [csharp]
var myDict = new Godot.Collections.Dictionary var myDict = new Godot.Collections.Dictionary
{ {
{"First Array", new Godot.Collections.Array{1, 2, 3, 4}} { "First Array", new Godot.Collections.Array { 1, 2, 3, 4 } }
}; };
[/csharp] [/csharp]
[/codeblocks] [/codeblocks]
To add a key to an existing dictionary, access it like an existing key and assign to it: To add a key to an existing dictionary, access it like an existing key and assign to it:
[codeblocks] [codeblocks]
[gdscript] [gdscript]
var points_dict = {"White": 50, "Yellow": 75, "Orange": 100} var points_dict = { "White": 50, "Yellow": 75, "Orange": 100 }
points_dict["Blue"] = 150 # Add "Blue" as a key and assign 150 as its value. points_dict["Blue"] = 150 # Add "Blue" as a key and assign 150 as its value.
[/gdscript] [/gdscript]
[csharp] [csharp]
var pointsDict = new Godot.Collections.Dictionary var pointsDict = new Godot.Collections.Dictionary
{ {
{"White", 50}, { "White", 50 },
{"Yellow", 75}, { "Yellow", 75 },
{"Orange", 100} { "Orange", 100 },
}; };
pointsDict["Blue"] = 150; // Add "Blue" as a key and assign 150 as its value. pointsDict["Blue"] = 150; // Add "Blue" as a key and assign 150 as its value.
[/csharp] [/csharp]
@ -104,29 +104,29 @@
"String Key": 5, "String Key": 5,
4: [1, 2, 3], 4: [1, 2, 3],
7: "Hello", 7: "Hello",
"sub_dict": {"sub_key": "Nested value"}, "sub_dict": { "sub_key": "Nested value" },
} }
[/gdscript] [/gdscript]
[csharp] [csharp]
// This is a valid dictionary. // This is a valid dictionary.
// To access the string "Nested value" below, use `((Godot.Collections.Dictionary)myDict["sub_dict"])["sub_key"]`. // To access the string "Nested value" below, use `((Godot.Collections.Dictionary)myDict["sub_dict"])["sub_key"]`.
var myDict = new Godot.Collections.Dictionary { var myDict = new Godot.Collections.Dictionary {
{"String Key", 5}, { "String Key", 5 },
{4, new Godot.Collections.Array{1,2,3}}, { 4, new Godot.Collections.Array { 1, 2, 3 } },
{7, "Hello"}, { 7, "Hello" },
{"sub_dict", new Godot.Collections.Dictionary{{"sub_key", "Nested value"}}} { "sub_dict", new Godot.Collections.Dictionary { { "sub_key", "Nested value" } } },
}; };
[/csharp] [/csharp]
[/codeblocks] [/codeblocks]
The keys of a dictionary can be iterated with the [code]for[/code] keyword: The keys of a dictionary can be iterated with the [code]for[/code] keyword:
[codeblocks] [codeblocks]
[gdscript] [gdscript]
var groceries = {"Orange": 20, "Apple": 2, "Banana": 4} var groceries = { "Orange": 20, "Apple": 2, "Banana": 4 }
for fruit in groceries: for fruit in groceries:
var amount = groceries[fruit] var amount = groceries[fruit]
[/gdscript] [/gdscript]
[csharp] [csharp]
var groceries = new Godot.Collections.Dictionary{{"Orange", 20}, {"Apple", 2}, {"Banana", 4}}; var groceries = new Godot.Collections.Dictionary { { "Orange", 20 }, { "Apple", 2 }, { "Banana", 4 } };
foreach (var (fruit, amount) in groceries) foreach (var (fruit, amount) in groceries)
{ {
// `fruit` is the key, `amount` is the value. // `fruit` is the key, `amount` is the value.
@ -298,7 +298,7 @@
[/codeblocks] [/codeblocks]
In GDScript, this is equivalent to the [code]in[/code] operator: In GDScript, this is equivalent to the [code]in[/code] operator:
[codeblock] [codeblock]
if "Godot" in {"Godot": 4}: if "Godot" in { "Godot": 4 }:
print("The key is here!") # Will be printed. print("The key is here!") # Will be printed.
[/codeblock] [/codeblock]
[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]. [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,7 +310,7 @@
<description> <description>
Returns [code]true[/code] if the dictionary contains all keys in the given [param keys] array. Returns [code]true[/code] if the dictionary contains all keys in the given [param keys] array.
[codeblock] [codeblock]
var data = {"width" : 10, "height" : 20} var data = { "width": 10, "height": 20 }
data.has_all(["height", "width"]) # Returns true data.has_all(["height", "width"]) # Returns true
[/codeblock] [/codeblock]
</description> </description>
@ -321,14 +321,14 @@
Returns a hashed 32-bit integer value representing the dictionary contents. Returns a hashed 32-bit integer value representing the dictionary contents.
[codeblocks] [codeblocks]
[gdscript] [gdscript]
var dict1 = {"A": 10, "B": 2} var dict1 = { "A": 10, "B": 2 }
var dict2 = {"A": 10, "B": 2} var dict2 = { "A": 10, "B": 2 }
print(dict1.hash() == dict2.hash()) # Prints true print(dict1.hash() == dict2.hash()) # Prints true
[/gdscript] [/gdscript]
[csharp] [csharp]
var dict1 = new Godot.Collections.Dictionary{{"A", 10}, {"B", 2}}; var dict1 = new Godot.Collections.Dictionary { { "A", 10 }, { "B", 2 } };
var dict2 = new Godot.Collections.Dictionary{{"A", 10}, {"B", 2}}; var dict2 = new Godot.Collections.Dictionary { { "A", 10 }, { "B", 2 } };
// Godot.Collections.Dictionary has no Hash() method. Use GD.Hash() instead. // Godot.Collections.Dictionary has no Hash() method. Use GD.Hash() instead.
GD.Print(GD.Hash(dict1) == GD.Hash(dict2)); // Prints True GD.Print(GD.Hash(dict1) == GD.Hash(dict2)); // Prints True

View File

@ -272,7 +272,7 @@
[b]Note:[/b] You must implement [method _get_plugin_name] for the state to be stored and restored correctly. [b]Note:[/b] You must implement [method _get_plugin_name] for the state to be stored and restored correctly.
[codeblock] [codeblock]
func _get_state(): func _get_state():
var state = {"zoom": zoom, "preferred_color": my_color} var state = { "zoom": zoom, "preferred_color": my_color }
return state return state
[/codeblock] [/codeblock]
</description> </description>

View File

@ -58,10 +58,10 @@
var propertyInfo = new Godot.Collections.Dictionary var propertyInfo = new Godot.Collections.Dictionary
{ {
{"name", "category/propertyName"}, { "name", "category/propertyName" },
{"type", Variant.Type.Int}, { "type", Variant.Type.Int },
{"hint", PropertyHint.Enum}, { "hint", PropertyHint.Enum },
{"hint_string", "one,two,three"} { "hint_string", "one,two,three" },
}; };
settings.AddPropertyInfo(propertyInfo); settings.AddPropertyInfo(propertyInfo);

View File

@ -99,7 +99,7 @@
Generates a GET/POST application/x-www-form-urlencoded style query string from a provided dictionary, e.g.: Generates a GET/POST application/x-www-form-urlencoded style query string from a provided dictionary, e.g.:
[codeblocks] [codeblocks]
[gdscript] [gdscript]
var fields = {"username": "user", "password": "pass"} var fields = { "username": "user", "password": "pass" }
var query_string = http_client.query_string_from_dict(fields) var query_string = http_client.query_string_from_dict(fields)
# Returns "username=user&amp;password=pass" # Returns "username=user&amp;password=pass"
[/gdscript] [/gdscript]
@ -112,7 +112,7 @@
Furthermore, if a key has a [code]null[/code] value, only the key itself is added, without equal sign and value. If the value is an array, for each value in it a pair with the same key is added. Furthermore, if a key has a [code]null[/code] value, only the key itself is added, without equal sign and value. If the value is an array, for each value in it a pair with the same key is added.
[codeblocks] [codeblocks]
[gdscript] [gdscript]
var fields = {"single": 123, "not_valued": null, "multiple": [22, 33, 44]} var fields = { "single": 123, "not_valued": null, "multiple": [22, 33, 44] }
var query_string = http_client.query_string_from_dict(fields) var query_string = http_client.query_string_from_dict(fields)
# Returns "single=123&amp;not_valued&amp;multiple=22&amp;multiple=33&amp;multiple=44" # Returns "single=123&amp;not_valued&amp;multiple=22&amp;multiple=33&amp;multiple=44"
[/gdscript] [/gdscript]
@ -148,7 +148,7 @@
To create a POST request with query strings to push to the server, do: To create a POST request with query strings to push to the server, do:
[codeblocks] [codeblocks]
[gdscript] [gdscript]
var fields = {"username" : "user", "password" : "pass"} var fields = { "username": "user", "password": "pass" }
var query_string = http_client.query_string_from_dict(fields) var query_string = http_client.query_string_from_dict(fields)
var headers = ["Content-Type: application/x-www-form-urlencoded", "Content-Length: " + str(query_string.length())] var headers = ["Content-Type: application/x-www-form-urlencoded", "Content-Length: " + str(query_string.length())]
var result = http_client.request(http_client.METHOD_POST, "/index.php", headers, query_string) var result = http_client.request(http_client.METHOD_POST, "/index.php", headers, query_string)

View File

@ -42,10 +42,10 @@
var propertyInfo = new Godot.Collections.Dictionary var propertyInfo = new Godot.Collections.Dictionary
{ {
{"name", "category/propertyName"}, { "name", "category/propertyName" },
{"type", (int)Variant.Type.Int}, { "type", (int)Variant.Type.Int },
{"hint", (int)PropertyHint.Enum}, { "hint", (int)PropertyHint.Enum },
{"hint_string", "one,two,three"}, { "hint_string", "one,two,three" },
}; };
ProjectSettings.AddPropertyInfo(propertyInfo); ProjectSettings.AddPropertyInfo(propertyInfo);