Using TypeScript in Defold game engine


#1

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:


An endless runner follows a pattern? (SOLVED)
#2

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


#3

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

#4

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.


#5

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.


#6

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


#7

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

You can download working project on GitHub


#8

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.

#9

Short video to show how it works.


#10

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 …


#11

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:


#12

Okay…sounds good :yum:


#13

wow! It’s very interesting