Godot Engine: Editing GDScript With Emacs

Posted:

Godot Engine is an exquisitely powerful, free game engine. One of my favorite aspects of Godot is that its editor was created with Godot itself! And indeed, it's an excellent showcase of how the engine could be used for building a great GUI program. But as excellent as the editor is, including the code editor for the bespoke scripting language GDScript, as an Emacs user it doesn't quite fit into my workflow. Thankfully, Godot has support for the Language Server Protocol, so I'm able to use Emacs in my game development workflow. In this post I'll describe what's required to do this, and how it compares to the stock Godot editor.

Why Godot?

There are countless options for game development, including robust engines such as: Game Maker, Unreal, Unity, and others, as well as more general and less featureful libraries that allow more freedom to do things in a specific way. Given these options, why choose Godot? I initially decided to try out Godot because I loved the fact that it was a totally free, permissably-licensed game engine that seemed pretty powerful.

With the help of some great learning resources such as GDQuest, I've gone from knowing almost nothing about creating games to being proficient enough to produce several small game demos. With this experience under my belt, I'm happy to say that Godot is not only conceptually awesome, but it's actually a joy to work with and a lot of fun.

I've only written with GDScript, which will feel very familiar to those who know a bit of Python. However there's also GDNative, which allows writing C or C++, Mono support if you prefer C#, and no matter what language you choose to go with, it's relatively simple to export your game to a variety of platforms including: Linux, macOS, Windows, HTML5, and Android.

Why GDScript?

Since Godot allows writing code in C, C++, and even C#, why would I want to use a custom language that's specific to the engine? Indeed, projects that demand high levels of performance could benefit greatly from skipping GDScript and going for a known, established alternative. But for me GDScript feels very familiar, like a "better Python" almost. Additionally: for the 2D games that I'm making, it should be more than performant enough to get the job done. 1

Why Emacs?

If Godot is so great, why would I bother going through the extra work to use Emacs instead of its built-in code editor (which again, is quite nice)? It's a matter workflow, for me; the vast majority of my general text editing and basically all of my code writing is done in Emacs. I've got a customized setup including many personal changes and keybindings, so it's quite jarring to use another editor for any reason. Even if I think to myself "I'll just deal with the differences", I will inevitably find myself using Emacs-specific keybindings and patterns, and my workflow will be disrupted. Given the availability of LSP support for both Emacs and Godot, it just makes sense for me to go this route.

GDScript X Emacs

With motivation out of the way: let's talk about the "how", starting with what's needed on the Emacs side of things and then moving on to what you need to do in the Godot editor itself.

Emacs Setup

The list of requirements for Emacs is somewhat short, but it isn't without options.

lsp-mode (or eglot)

The first thing you'll need is a functional LSP setup - with either lsp-mode or eglot. Going into the difference between these two and how to set either of them up, is outside of the scope of this post. I'm personally using lsp-mode rather than eglot, but I'm sure eglot would be just as functional. 2

gdscript-mode

Next up you'll need gdscript mode. Aside from autocompletion offered by LSP integration, there are a few extra goodies that are worth considering:

Emacs Daemon

Running an Emacs daemon isn't strictly required, but I found that without it anytime I opened a new script, a totally new instance of Emacs would be opened. With the daemon, you can easily open new scripts in an existing Emacs frame (if you have one already open). I've written about setting up the Emacs daemon before, so please refer to those posts if you want a reference. 3

At this point, you should be ready to configure Godot to use an external editor.

Godot Editor Setup

It's important to note that Emacs will not totally replace the Godot editor; you'll still use that for the scene editor and other things such as audio and animation editing. Emacs will take over code-editing duties, as well as debugging code.

From the main Godot editor window, click the Editor menu, then Editor Settings.... This will open the "Editor Settings" window. In the lefthand pane scroll down to the "Text Editor" section and click "External". Check the box next to "Use External Editor", enter emacsclient for the "Exec Path" option, and --socket-name=SOCKET_NAME_HERE -n {file} +{line} for the "Exec Flags" option (changing the socket name to whatever yours is).

That should be it; go ahead close the "Editor Settings" window, and open a script (make sure you've got an existing Emacs frame already opened). Emacs should take focus with the script you clicked open, with all the LSP features at your fingertips.

Results

And now for the burning questions: how is it, and is it good enough to use instead of the stock Godot editor?

I'm pleased to report that: it's pretty great, and yes it is! But it isn't a one-to-one experience. Here are some problems I've encountered:

As I re-read over the points above, it does sound kinda bleak. But honestly the overall experience is awesome. Even with the quirks I mentioned above, being able to integrate Emacs with the Godot editor as much as I can is really huge for me. If I wasn't so invested in Emacs I'm not sure if the experience would be acceptable, but as an Emacs user I love what we've got and am looking forward to building out and improving the ecosystem.

I'll also update this post when I finish the demo I'm currently working on, just to give readers a sample of what I'm trying to do here. For now, have a gif of me writing some GDScript with Emacs: 5

Footnotes And References

1 I love Python, but GDScript's tight integration with Godot is what really makes it great in my opinion.

2 Please refer to the relevant documentation for setting up either lsp-mode or eglot. If you aren't sure which you want, even after reading about both, just go with lsp-mode and revisit the question later on.

3 See: Emacs daemon as a runit service and Emacs daemon as a runit "user service"

4 This also results in a somewhat visually jarring experience. At least compared to the experience I have with other languages and lsp-mode, such as Go.

5 This gif was created with emacs-gif-screencast. The code is based on GDQuest's Make Pro 2d Games with Godot Open Source A-RPG Demo project. Big thanks and a lot of respect to the GDQuest folks!

This page was last modified on: 2021-12-24