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.


Since my last post, I was able to get in touch with the maintainer of the TS-Defold project and contribute several of my changes. Thanks to Justin for being patient with my flurry of pull requests.

Additionally, I worked on several other projects:

1 Like

Hehe, this is cool. Boom is my Lua/Defold game framework, heavily inspired by Kaboom.js, a JavaScript game framework. And now we have TypeScript bindings for Boom.

Will you contribute these changes back to the main repo?

What is your take on TS-Defold? Is it useful? What’s good and bad?

1 Like

The only big drawback to TS-Defold is that types aren’t automatically generated for Defold extensions. There is experimental support for parsing script_api files for this purpose, but it’s not built into the starter templates… yet.

Coding in TypeScript for Defold is pretty intuitive. As long as you use language features that are shared between Lua and TS (no enums, switch statements, etc), your output Lua code is very clean and easy to follow.

TypeScript has a great ecosystem for tools. A lot of the mental burden of thinking about code correctness can be automated away. Using an IDE like VSCode, you get real-time type safety checks, auto-completion, etc. with very little setup. For bigger projects, you can add real-time linting rules for code correctness and style, automatic formatting, etc.

If my changes aren’t too controversial, yes.

1 Like

This sounds like a great addition!

Just joined the forum and found this thread. Looking over GitHub - thinknathan/ts-defold-types: TypeScript definitions for the game engine Defold, with hand-written additions which looks like it will be very useful to what I want to do, which is basically create an abstract VM definition in TypeScript which could either be implemented using other popular ES6-based game engines, or implemented using Defold.

1 Like

I don’t have a background in computer science, so terms like “abstract VM definition” are a little over my head. Is it a game framework like how brtizl’s Platypus gives you the API to make a platformer game?

You may want to check out the Caveats section of the TypeScriptToLua project. As long as you don’t need those particular features, there’s a good chance your TypeScript can be transpiled for Defold. There’s also no equivalent to web workers in Defold, so all logic must be single-threaded.

Good luck with your project! Feel free to share your progress in a new thread or visit the TypeScript-Defold discord.

I was able to finish this project, creating a tool that produces TypeScript definitions from script_api files. I’ve got a test that runs on 17 real extensions, and it generates valid types for all of them. Some are rougher than others, depending on how much detail was put into the original script_api.

Lua-only extensions can still pose an issue, but we’ve got pre-defined definitions for a lot of them. I’m working my way through the list of popular extensions and writing types by hand when needed. Types for Monarch are almost ready.


Have you also seen ?

1 Like

We’ve got about 40% of the popular extensions ready to work with ts-defold. I’ve opened an issue to track progress.

If anyone in the community wants to help, you don’t need to know anything about TypeScript. Just add a really accurate script_api file to your Defold library (or contribute one to another popular library). Parsing the script_api file gets us most of what we need, and hopefully everyone benefits. :grinning: