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

  • From: "Victorious" <dtvictorious@xxxxxxxxx>
  • To: <ian-reeds-games@xxxxxxxxxxxxx>
  • Date: Sat, 28 Mar 2015 10:37:11 +0800

Hi Craig

 

I’d love to see that file. How much work would it be to convert my existing 
scripts (about 700 lines of code or so) into typescript and use the modules 
feature and such? I’d be nice not to always have things in the global 
namespace. I have vs 2013 community edition installed.

 

Victorious

From: ian-reeds-games-bounce@xxxxxxxxxxxxx 
[mailto:ian-reeds-games-bounce@xxxxxxxxxxxxx] On Behalf Of Craig Brett 
(Redacted sender "craigbrett17@xxxxxxx" for DMARC)
Sent: Saturday, March 28, 2015 5:25 AM
To: ian-reeds-games@xxxxxxxxxxxxx
Subject: [ian-reeds-games] Re: For scripters: Typescript

 

I don't think I'll need to update the scripting documentation, I'm not changing 
anything from the ScriptApi side. It's just on my machine. Typescript spits out 
Javascript when it's compiled, so really its just a way of organising your 
Javascript, especially when working with applications or on a larger scale. 

Welcome to the JavaScript world! You're doing great for your first foray into 
it.

If you wanted to try Typescript, you wouldn't feel too far from home with some 
Java experience. It supports classes, interfaces, static members, inheritance, 
etc. And importantly type safety, though it is optional type safety. 

As for modules, they're what C# call namespaces and I think what Java call 
packages. It's been a while since I done them in Java. But they're essentially 
separate spaces where you can store exported types (classes, interfaces). With 
Typescript, you can also store functions directly in them, unlike C#/Java. 
Which is something I only learned recently. 

So instead of having all your functions and variables littered around the 
global namespace, you can put them inside an object to keep it all organised. 
So instead of: change_unit_type(stuff)
I now have: Transformation.change_unit_type(stuff)

It doesn't look much different, but I find it a little neater. Now I've done 
the initial work to get TS going for my scripts and making that type 
definitions file. 

That file is practically done now, aside from possibly using some JSDoc to 
describe things in there or me possibly forgetting something. Let me know if 
you ever fancy giving TS a go and I'll send the file over. 

On 27/03/2015 08:58, Victorious wrote:

Hi Craig,

 

That looks pretty interesting. I look forward to reading the definition files 
and the updated scripting documentation when that’s ready. I don’t actually 
have any prior experience with javascript (I wrote my first line of javascript 
only about 2 weeks or so ago) but have some C and java background, so I’ll have 
some catching up to do with typescript. What are modules? Are they like methods 
in classes? 

 

Victorious

 

From: ian-reeds-games-bounce@xxxxxxxxxxxxx 
[mailto:ian-reeds-games-bounce@xxxxxxxxxxxxx] On Behalf Of Craig Brett 
(Redacted sender  <mailto:craigbrett17@xxxxxxx> "craigbrett17@xxxxxxx" for 
DMARC)
Sent: Friday, March 27, 2015 7:33 AM
To: ian-reeds-games@xxxxxxxxxxxxx
Subject: [ian-reeds-games] Re: For scripters: Typescript

 

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

 

 

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Other related posts: