Skip to content

Commit

Permalink
Tweak guidelines on dictionary and enum formatting in GDScript style …
Browse files Browse the repository at this point in the history
…guide

- Recommend writing each enum item on its own line, similar to C++.
- Recommend using spaces around inline dictionary declarations
  to make them easier to distinguish from arrays (due to similar-looking
  `[]` and `{}` characters in most fonts). This is commonly found
  in Python or JavaScript style guides.
  • Loading branch information
Calinou committed May 29, 2024
1 parent f5bc8b1 commit a713eeb
Showing 1 changed file with 49 additions and 18 deletions.
67 changes: 49 additions & 18 deletions tutorials/scripting/gdscript/gdscript_styleguide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Here is a complete class example based on these guidelines:
## Initializes states and delegates engine callbacks ([method Node._physics_process],
## [method Node._unhandled_input]) to the state.


signal state_changed(previous, new)

@export var initial_state: Node
Expand All @@ -46,7 +45,6 @@ Here is a complete class example based on these guidelines:
set = set_state
@onready var _state_name = _state.name


func _init():
add_to_group("state_machine")

Expand Down Expand Up @@ -232,25 +230,23 @@ line doesn't need to be modified when adding new elements.

::

enum Tiles {
TILE_BRICK,
TILE_FLOOR,
TILE_SPIKE,
TILE_TELEPORT,
}
var array = [
1,
2,
3,
]

**Bad**:

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

::

enum Tiles {
TILE_BRICK,
TILE_FLOOR,
TILE_SPIKE,
TILE_TELEPORT
}
var array = [
1,
2,
3
]

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

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

::

enum Tiles {TILE_BRICK, TILE_FLOOR, TILE_SPIKE, TILE_TELEPORT}
var array = [1, 2, 3]

**Bad**:

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

::

enum Tiles {TILE_BRICK, TILE_FLOOR, TILE_SPIKE, TILE_TELEPORT,}
var array = [1, 2, 3,]

Blank lines
~~~~~~~~~~~
Expand Down Expand Up @@ -485,7 +481,11 @@ Whitespace
~~~~~~~~~~

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

**Good**:

Expand All @@ -497,6 +497,7 @@ in dictionary references and function calls.
position.y = target_position.y + 10
dict["key"] = 5
my_array = [4, 5, 6]
my_dictionary = { key = "value" }
print("foo")

**Bad**:
Expand All @@ -509,6 +510,7 @@ in dictionary references and function calls.
position.y = mpos.y+10
dict ["key"] = 5
myarray = [4,5,6]
my_dictionary = {key = "value"}
print ("foo")

Don't use spaces to align expressions vertically:
Expand Down Expand Up @@ -704,6 +706,29 @@ are constants:
FIRE,
}

Write enums with each item on its own line. This allows adding documentation comments abve each item
more easily, and also makes for cleaner diffs in version control when items are added or removed.

**Good**:

.. rst-class:: code-example-good

::

enum Element {
EARTH,
WATER,
AIR,
FIRE,
}

**Bad**:

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

::

enum Element { EARTH, WATER, AIR, FIRE }

Code order
----------
Expand Down Expand Up @@ -791,7 +816,13 @@ variables, in that order.

signal player_spawned(position)

enum Jobs {KNIGHT, WIZARD, ROGUE, HEALER, SHAMAN}
enum Jobs {
KNIGHT,
WIZARD,
ROGUE,
HEALER,
SHAMAN,
}

const MAX_LIVES = 3

Expand Down

0 comments on commit a713eeb

Please sign in to comment.