Home

Awesome

cc-tstl-template

Template project for ComputerCraft programs written in TypeScript. Uses TypeScriptToLua to compile with ComputerCraft typing declarations.

Usage

  1. Clone the repository (or download the ZIP). You can also use the Use this template button on GitHub to fork the repo directly.
  2. Run npm install to install dependencies, including TypeScriptToLua.
  3. Customize package.json if you want - it's not used in CC.
  4. Add your code to main.ts, and add other files as desired.
  5. Build the project with npm run build.
  6. Copy main.lua to ComputerCraft, either by copying into the computer folder, dropping on the terminal, using Pastebin, or with CraftOS-PC Remote or cloud-catcher.

Libraries

Built-in CraftOS APIs

All base CraftOS APIs are available in the global namespace. Peripherals are also implemented as classes that inherit from the IPeripheral interface, so you can call wrap and cast it to the desired class to get typings for the peripheral.

cc.* Modules

All modules available in /rom/modules/main/cc have typings included. To import them, just use the import statement like normal:

import * as strings from "cc.strings";
// ...
let str = strings.ensure_width(arg, 20);

Events

A library for handling events in a nicer way (e.g. using named properties rather than indexes) is included as a separate source file. The first line in main.ts includes the event library, which has classes for each event type as well as functions that can automatically or manually pull events with the specified type.

Example:

import * as event from "./event";

const timer = os.startTimer(5);
while (true) {
    const ev = event.pullEventAs(event.TimerEvent, "timer");
    if (ev.id == timer) break;
}

All types are included in the compiled output, even if they were never used. To avoid this, comment out the event class declarations you don't need, and remove the init functions from eventInitializers. Do not remove GenericEvent, as this is the fallback event type when other types aren't available.

tsconfig.json Options

The tsconfig.json file contains some options used by TypeScriptToLua to adjust how Lua code is generated. Some of these may be useful for CC development. See the TSTL webpage for the rest of the options.

License

The typings in types/ and event.ts are licensed under the MIT license. Projects are free to use these files as provisioned under the license (i.e. do whatever you want, but include the license notice somewhere in your project). Anything else is public domain.