stevenlafl / tts-typescript

Typescript Declarations for Tabletop Simulator
MIT License
7 stars 2 forks source link

I need a guide on how to use the types 🦺 #1

Closed AucaCoyan closed 1 year ago

AucaCoyan commented 1 year ago

Hi! I'm very interested in using this library to build my lua scripts. Unfortunately I couldn't make it work :( I feel strange with the

/// <reference path="./backgrounds.d.ts" />

all over the types.

Can you lend me a hand and help to setup a simple sample? I would like to contribute if I'm able!

stevenlafl commented 1 year ago

Hello, in a new project try this:

npm i tts-types typescript typescript-to-lua
mkdir src

Create tsconfig.json:

{
  "compilerOptions": {
    "rootDir": ".",
    "outDir": "build",

    "target": "ESNext",
    "lib": ["ESNext"],

    "moduleResolution": "nodenext",

    "declaration": false,
    "declarationMap": false,

    "strict": true,
    "types": ["tts-types"],
  },
  "tstl": {
    "luaTarget": "5.2",
    "noImplicitSelf": true,
    "tstlVerbose": true,
    "luaBundle": "bundle.lua",
    "luaBundleEntry": "src/index.ts",
  },
}

Create src/index.ts:

// The OnLoad function. This is called after everything in the game save finishes loading.
// Most of your script code goes here.
function onLoad( saveData: any ) {

  // Lock color-UI cube
  let cube = getObjectFromGUID( "c1a0d1" )
  cube.interactable = false
  cube.setLock( true )
}

Now run:

npx tstl

You'll get something like this:

Loaded 0 plugins
Parsing project settings
Transforming C:/Source/tts-test/src/index.ts
Printing C:/Source/tts-test/src/index.ts
Constructing emit plan
Resolving dependencies for C:/Source/tts-test/src/index.ts
Emitting output
Emitting C:/Source/tts-test/build/bundle.lua
Emit finished!

Your .lua file now exists at build/bundle.lua:

--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]

local ____modules = {}
local ____moduleCache = {}
local ____originalRequire = require
local function require(file, ...)
    if ____moduleCache[file] then
        return ____moduleCache[file].value
    end
    if ____modules[file] then
        local module = ____modules[file]
        ____moduleCache[file] = { value = (select("#", ...) > 0) and module(...) or module(file) }
        return ____moduleCache[file].value
    else
        if ____originalRequire then
            return ____originalRequire(file)
        else
            error("module '" .. file .. "' not found")
        end
    end
end
____modules = {
["src.index"] = function(...) 
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
function onLoad(saveData)
    local cube = getObjectFromGUID("c1a0d1")
    cube.interactable = false
    cube.setLock(true)
end
 end,
}
return require("src.index", ...)

I use this to copy it in automatically (package.json):

  "scripts": {
    ...
    "compile": "tstl && npm run deploy",
    "deploy": "copy \"build\\bundle.lua\" \"%LOCALAPPDATA%\\Temp\\TabletopSimulator\\Tabletop Simulator Lua\\Global.-1.lua\""
    ...
  }

So I can simply use npm run compile

AucaCoyan commented 1 year ago

Works like a charm! Let me do a PR with this guide 😄 . Be right back.

stevenlafl commented 1 year ago

Awesome to hear! I'm super interested in seeing it used in an actual production game. I was mainly interested in creating this as an exercise in writing extensive custom type definitions, so I only tested basic examples in Tabletop Simulator. If you encounter more problems, let me know.

AucaCoyan commented 1 year ago

Great! I'll try some things and let you know. Thanks!