Format your Code with GDFormat

beginner

By: Nathan Lovato

Never format your code by hand again! Let GDFormat do it for you.

Godot 3.2 is lacking a code formatter for GDScript. Formatting your code by hand is a time-consuming process, time you could instead use to code some more.

Thankfully, Pawel Lampe wrote a Python program to save the day, and that’s what we’re looking at in this short tutorial.

GDFormat takes code like this:

enum States { IDLE,
			  RUN,
			  AIR,
			  LAND
			  }

onready var animation_tree  : AnimationTree = $AnimationTree
onready var _playback        : AnimationNodeStateMachinePlayback = animation_tree["parameters/playback"]



func _ready() -> void:
	animation_tree.active      = true

And turns it into that:

enum States { IDLE, RUN, AIR, LAND }

onready var animation_tree: AnimationTree = $AnimationTree
onready var _playback: AnimationNodeStateMachinePlayback = animation_tree["parameters/playback"]


func _ready() -> void:
	animation_tree.active = true

Installing the GDScript Toolkit

GDFormat is part of the GDScript Toolkit, a set of three programs:

  1. gdparser, a parser for GDScript.
  2. gdlint, a code linter.
  3. gdformat, a code formatter.

They all come together in one Python package that you can install via pip, Python’s package manager, using your terminal:

# On Windows
pip install gdtoolkit

# On MacOS and Linux
pip3 install gdtoolkit

Once that’s done, you can format any file by calling the command gdformat:

# Format a single file
gdformat path/to/file.gd

# Format all gdscript files in the current directory
gdformat *.gd

# Find all gdscript files recursively and format them
# In Bash
gdformat $(find . -name '*.gd')
# In Fish
gdformat **.gd

That’s it!

Known limitations

GDFormat should work great with most GDScript code. At the time of writing, there are some exotic bits of GDScript syntax it doesn’t support. For example, defining variables in match patterns:

# Executed when entering a State in the StateMachine
func enter(msg: Dictionary = {}) -> void:
	match msg:
		{"velocity": var v, "jump_impulse": var ji}:
			_parent.velocity = v + Vector3(0, ji, 0)

But in general, it works well.

Become a better game developer

Join our weekly newsletter and get our latest game creation tutorials, tips, and open-source tools right in your inbox.

🔒 No spam. Unsubscribe anytime.

Made by

Nathan Lovato

GDQuest founder. Courteous designer with a taste for Free Software. I promote sharing and collaboration.