[ian-reeds-games] Re: For scripters: Typescript

  • From: "Craig Brett" <dmarc-noreply@xxxxxxxxxxxxx> (Redacted sender "craigbrett17@xxxxxxx" for DMARC)
  • To: ian-reeds-games@xxxxxxxxxxxxx
  • Date: Thu, 26 Mar 2015 23:33:29 +0000

As a bit of early feedback. I spent a few hours doing this tonight. And it's all good!


Took a while getting through the errors where I'd not been quite doing things properly (using var x in array), etc. And more commonly where I was setting up submodules.

One positive upshot here is it should be highly unlikely, with the exception of the event handlers that have to be in the global namespace, to get a naming conflict, since all my stuff will be under the CB module, and various submodules within that.

The real win for me though is being able to know that an event argument has a unit and saying so. Then when I use that variable, I can just get all the unit specific methods and fields in intellisense. And knowing I can't mistype things now always helps, too.

It all seems to still work fine (after I worked out that the game needed event subscriptions to be in the global namespace). I won't be releasing this version in the wild until there's a new feature to show, though. Which should hopefully give me more testing time to make sure all my scripts work. Age Of Warlords, Demon Wars and some custom fiddling of my own should give me enough testing, and hey it will be fun too.

On 26/03/2015 20:01, Craig Brett (Redacted sender craigbrett17@xxxxxxx for DMARC) wrote:
Hi all,

This is more something for either the scripters or those who are interested in code organisation and things of such a geeky nature. If you're not that way inclined, feel free to move on.

I started work on some more scripts today, and realised I could try something new to help myself and help my code organization as well. Using TypeScript <http://www.typescriptlang.org/>. It's a superset of Javascript that compiles into Javascript itself. I use it at work and I know Ian is a big fan of it too. It offers a lot of cool things that native JS doesn't or does but with some more effort.

So to aid me in this, I was going to make some definition files for some of the in game types, such as shared, unit, etc. And then wondered whether it was worth sharing them or not. And then decided some of it would be less useful depending on what you use to write your javascript.

I used to use Notepad++, but now I use Visual Studio to handle all my scripting (as well as my .md files), for various reasons, not least code completion, etc. And also being able to autocomplete on referenced files outside of the current file. As well as a build step which I made which zips up my js files and docs and copies them into the right places.

Typescript makes all this even better. One of the features is being able to make definition files of types outside your code, so in this case the TB back end script API that the game exposes to us. I'm happy to make the ones from the documentation and share them if you guys think they'd come in handy for you.

Another cool thing Typescript easily lets you do is put things neatly into modules. You can already do this in native Javascript, of course, using self-executing functions and such, but it's much nicer in Typescript. I'll give the example I'm currently working on.

module CraigBrett.AI {
    export function preferencialSkillOrder(event: any): void {

    }
}

And here's the slightly less attractive JavaScript it compiles to.

var CraigBrett;
(function (CraigBrett) {
    var AI;
    (function (AI) {
        function preferencialSkillOrder(event) {
        }
        AI.preferencialSkillOrder = preferencialSkillOrder;
    })(AI = CraigBrett.AI || (CraigBrett.AI = {}));
})(CraigBrett || (CraigBrett = {}));

It also supports static typing, so things have a real type, although you can get around this by using any (as I did above). Static typing is better if you can do it, as it helps you catch errors early. But I get that it's not for everyone.

I'm not sure how my TB Typescripting for scripts will go, but I thought I'd share my experiment with the other scripters to see what you think.

To add to this, what tools do you guys use to write your scripts? I'm just curious. And also wondering whether my TS stuff will help any of you.

Regards,
Craig

Other related posts: