Using TypeScript in Defold game engine

Hi, I try to use TypeScript as scripting lang in Defold. A made some tests and it works!. Basic idea - Transpile TypeScript to Lua.

As benefits we have TypeScript and VSCode with incredible TS support. TS is very popular in web development now and TS support in Defold make it best web game engine for many people who already known TS.

It’s early version with very basic functionally (currenly have only functions for example projects) but it works. A lot to do. Use an example project for basic tests. Do not hesitate to contribute! :grinning:

16 Likes

Not sure how to use it or how it can support Lua in full, but it is interesting.

1 Like

Do not need support Lua in full. It use basic Lua functions and construction to trinspile TypeScript. Generated Lua code is very clean and human readable.

Here is TS code.

import { Unit } from "./modules/unit";

let unit: Unit

function init(self: any) {
    print("The amazing world of Defold TypeScript")
    unit.create();
}

function update(self: any, dt: number) {
    unit.update(dt);
}

And here is output:

require("typescript_lualib")
local unit0 = require("modules.unit")
local Unit = unit0.Unit
local unit = nil

function init(self)
    print("The amazing world of Defold TypeScript")
    unit:create()
end
function update(self,dt)
    unit:update(dt)
end
2 Likes

Using TS you get strong type check, typed arguments, classes, inheritance and so on. All reflects to lua basic constructions. TS to Lua work the same way as TS to JS. Very usefulfor big projects support.

1 Like

Don’t see reasons why this shouldn’t work. I think there was a similar project for Defold but with Haxe - https://github.com/hxdefold/hxdefold. A nice feature would be to provide TS bindings to Defold API (generate it?) so you would get some code assistance while typing.

4 Likes

This is what we are currently working on. It’s not hard but require manual work (some decisions about types for functions arguments)

2 Likes

New working version is here. TypeScript file now have all declarations for Defold API. !

You can download working project on GitHub

10 Likes

Additionally I have create simple script that working in watch mode and prepare lua files automatically just when you save any TypeScript file from project. Process is fully automated now. Just 3 steps to repeat until game do not finished.

  • Write TypeScript code in Visual Studio Code.
  • Save it
  • Go to Editor and build game.
4 Likes

Short video to show how it works.

7 Likes

Very interesting. Does it mean, if you have JS coding expirience, you are very close to JS because of TypeScript, or is it a whole different story? Just in Terms of “feeling home / feeling familiar” with the language. I am very new to LUA, this is why i am asking …

3 Likes

You can use JS as TypeScript is a superset of JavaScript.
You can use JS and start to add type notations (firs steps to TS). :grinning:

1 Like

Okay…sounds good :yum:

1 Like

wow! It’s very interesting

I’m interested in checking out TypeScript in Defold to take advantage of some of its features in a game I’m working on - Are we able to mix Lua and Typescript? (And do you consider it a good move?) For example, if I would like to move some logic from Lua to Typescript and use OOP when designing some more complex objects for which many times I’m struggling with some problems I’m pretty sure I would neutralize (or at least detect more conveniently and faster) with type checking, compile time detections and so on.

1 Like

Yes, you can mix Lua and TS.
Check this site for more information/docs https://ts-defold.dev

2 Likes