Starting out with 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. I’m a web dev as my day job, so I was drawn to the TypeScript community extension for Defold.

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 comes with ts-defold, so you’re not locked to using early versions of TSTL. (Link)
  • 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. (Link)
  • Created type definitions for britzl’s new boom framework. (Link)
  • Created a project template that includes all of the above. (Link)
  • Created type definitions for thejustinwalsh’s xmath framework. The comments are adapted from Defold’s vmath library, so I’m not sure they’re 100% accurate to xmath’s implementation, but they seem pretty close. (Link)

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 works.

  • Math library with clamp and round functions (Link)

  • String library with a variety of convenience methods (Link)

  • PRNG library based on mulberry32 (Link)

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.

(Apologies for posting a follow-up so soon; this was supposed to be part of the initial post, but I ran into a limit with the number of links.)


Pretty cool stuff to be honest! Well done!

No worries. This was probably because you joined the forum recently and your trust level was low.

This is nice. Could this be directly integrated with ts-defold? Or is it ok to have it as a separate plugin?

Interesting. What kind of prompts did you use? How much manual work did you need to do?

1 Like

I think it’s fine to exist as a separate plugin. Yesterday I published it to the npm registry, so that should be a dependable place for it to live.

I’d like to update the main TS-Defold template to use my plugin. I’ve started the process, but the chain of dependencies is more complex than I first realized.

The TypeScript project doesn’t use semantic versioning. But the package managers in this ecosystem expect all packages to follow semver. So lots of TS-Defold dependencies are requesting TypeScript > 4 and < 5, because they expect v5 to be a major version with breaking changes.

I’m in the process of sending pull requests to several repos that will hopefully unblock the updates I’d like to make.

I would start my initial prompt with as many requirements as I could, like this:

"create a defold extension that exposes a module to lua, use c++ that does not use any features newer than 2009, and does not use the standard library, and always uses const char instead of std::string. the name of the module is estring."*

Then I would add specific functions in follow-up prompts.

It really likes to output code that uses “luaL_newlib”. I think this is from Lua 5.2 and so it doesn’t work in Defold? I would manually replace it with “luaL_register” because I had seen other Defold extensions use that. Maybe if I had told it to use Lua 5.1 it wouldn’t make that mistake.