Exploring Defold and TypeScript

The Defold game engine has been in my bookmarks folder for months now, but I’ve only just recently started to make things with the engine.

Defold fits a number of my criteria for a great game engine:

  • It exports to the web, and performance in the browser is actually good!
  • It effortlessly exports to all other platforms too
  • It’s well suited for 2D games (the editor can plop down objects at nice round X and Y coordinates)
  • It’s free and open-source (technically there are limitations in the Defold license, but nothing that affects making games)
  • It’s extremely well maintained with monthly updates

However, I was initially put off by the use of Lua as its scripting language. I had never used Lua, so that was a barrier to entry. It’s also a dynamically typed language, which is a pain to debug.

Then I discovered the TypeScript community extensions (@ts-defold) for Defold. These tools allow using Defold with TypeScript, a strongly typed language that I already know and enjoy.

It turns out I’m more productive working on tools than games, so here’s what I’ve done so far:

  • Created a TypeScriptToLua (TSTL) plugin that will strip the last extension from files that have multiple extensions. This is used to handle Defold’s specific file extensions, so a file name like `player.script.ts` is output to `player.script` instead of the incorrect `player.script.lua`. This plugin can replace the patch file that come with @ts-defold, so you’re no longer locked to early versions of TSTL.
  • Tweaked the type definitions for the Defold game engine from @ts-defold/types. I’ve been slowly describing more of the types that were left as unknown in the original output. I’m not sure how useful this is to the average developer, but I find it satisfying.
  • Created type definitions for @britzl’s new boom framework.
  • Created a project template that includes all of the above.
  • Created type definitions for @thejustinwalsh’s xmath framework. The comments are copied from Defold’s vmath library, so I’m not sure they’re 100% accurate to xmath’s implementation, but they seem pretty close.

I also played around with using Chat-GPT to generate some native extensions. My C++ knowledge is limited, but I was able to cajole the AI into creating something that actually works.

There are already community extensions that cover these use cases, so I doubt my libraries will see much use. Still, it’s fun to see how easy it is to extend the engine, despite being a complete newcomer.

Clones of Vampire Survivors are in the zeitgeist now, so I’m currently trying to create a really simple one of those. I’ll open-source the whole project when it’s done.