Skip to content

Commit 0a8bd35

Browse files
authored
Merge pull request #9435 from Calinou/gdscript-style-guide-dictionary-enum
Tweak guidelines on dictionary and enum formatting in GDScript style guide
2 parents f3efea5 + 71cdbdd commit 0a8bd35

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

tutorials/scripting/gdscript/gdscript_styleguide.rst

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Here is a complete class example based on these guidelines:
3535
## Initializes states and delegates engine callbacks ([method Node._physics_process],
3636
## [method Node._unhandled_input]) to the state.
3737

38-
3938
signal state_changed(previous, new)
4039

4140
@export var initial_state: Node
@@ -232,25 +231,23 @@ line doesn't need to be modified when adding new elements.
232231

233232
::
234233

235-
enum Tiles {
236-
TILE_BRICK,
237-
TILE_FLOOR,
238-
TILE_SPIKE,
239-
TILE_TELEPORT,
240-
}
234+
var array = [
235+
1,
236+
2,
237+
3,
238+
]
241239

242240
**Bad**:
243241

244242
.. rst-class:: code-example-bad
245243

246244
::
247245

248-
enum Tiles {
249-
TILE_BRICK,
250-
TILE_FLOOR,
251-
TILE_SPIKE,
252-
TILE_TELEPORT
253-
}
246+
var array = [
247+
1,
248+
2,
249+
3
250+
]
254251

255252
Trailing commas are unnecessary in single-line lists, so don't add them in this case.
256253

@@ -260,15 +257,15 @@ Trailing commas are unnecessary in single-line lists, so don't add them in this
260257

261258
::
262259

263-
enum Tiles {TILE_BRICK, TILE_FLOOR, TILE_SPIKE, TILE_TELEPORT}
260+
var array = [1, 2, 3]
264261

265262
**Bad**:
266263

267264
.. rst-class:: code-example-bad
268265

269266
::
270267

271-
enum Tiles {TILE_BRICK, TILE_FLOOR, TILE_SPIKE, TILE_TELEPORT,}
268+
var array = [1, 2, 3,]
272269

273270
Blank lines
274271
~~~~~~~~~~~
@@ -485,7 +482,11 @@ Whitespace
485482
~~~~~~~~~~
486483

487484
Always use one space around operators and after commas. Also, avoid extra spaces
488-
in dictionary references and function calls.
485+
in dictionary references and function calls. One exception to this is for
486+
single-line dictionary declarations, where a space should be added after the
487+
opening brace and before the closing brace. This makes the dictionary easier to
488+
visually distinguish from an array, as the ``[]`` characters look close to
489+
``{}`` with most fonts.
489490

490491
**Good**:
491492

@@ -497,6 +498,7 @@ in dictionary references and function calls.
497498
position.y = target_position.y + 10
498499
dict["key"] = 5
499500
my_array = [4, 5, 6]
501+
my_dictionary = { key = "value" }
500502
print("foo")
501503

502504
**Bad**:
@@ -509,6 +511,7 @@ in dictionary references and function calls.
509511
position.y = mpos.y+10
510512
dict ["key"] = 5
511513
myarray = [4,5,6]
514+
my_dictionary = {key = "value"}
512515
print ("foo")
513516

514517
Don't use spaces to align expressions vertically:
@@ -704,6 +707,29 @@ are constants:
704707
FIRE,
705708
}
706709

710+
Write enums with each item on its own line. This allows adding documentation comments abve each item
711+
more easily, and also makes for cleaner diffs in version control when items are added or removed.
712+
713+
**Good**:
714+
715+
.. rst-class:: code-example-good
716+
717+
::
718+
719+
enum Element {
720+
EARTH,
721+
WATER,
722+
AIR,
723+
FIRE,
724+
}
725+
726+
**Bad**:
727+
728+
.. rst-class:: code-example-bad
729+
730+
::
731+
732+
enum Element { EARTH, WATER, AIR, FIRE }
707733

708734
Code order
709735
----------
@@ -791,7 +817,13 @@ variables, in that order.
791817

792818
signal player_spawned(position)
793819

794-
enum Jobs {KNIGHT, WIZARD, ROGUE, HEALER, SHAMAN}
820+
enum Jobs {
821+
KNIGHT,
822+
WIZARD,
823+
ROGUE,
824+
HEALER,
825+
SHAMAN,
826+
}
795827

796828
const MAX_LIVES = 3
797829

0 commit comments

Comments
 (0)