Skip to content

Commit edc7743

Browse files
authored
feat: add a Defold API page (#32)
* fix: clarify value of lua target * fix: simpler, more clear language * feat: add page on defold API * fix: re-add mention of prettier * feat: finish API page * chore: update desc of export plugin to current
1 parent a50b21e commit edc7743

File tree

5 files changed

+69
-16
lines changed

5 files changed

+69
-16
lines changed

content/configuration/def-api.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: "Defold API"
3+
order: 3
4+
---
5+
6+
Getting accurate types for the Defold API is an important part of ts-defold. Here are some
7+
ways to get the definitions you need:
8+
9+
:sparkles: The `types` library is built-in into all ts-defold templates, and is automatically
10+
published to keep up with the latest changes to Defold.
11+
12+
:star2: [TS-Defold Types II](https://github.com/thinknathan/ts-defold-types)
13+
is a drop-in alternative for the `types` library, with hand-written patches to provide for
14+
more accurate and useful types.
15+
16+
:boom: [Defold Annotations for Typescript](https://github.com/elMuso/defold-annotations-typescript/)
17+
Is a tool for generating types based on the Defold API, with more accurate types and better coverage
18+
of Lua features than the default `types`.
19+
20+
# Working with Messages
21+
22+
The Defold engine is built around communication using [messages](https://defold.com/manuals/message-passing/),
23+
so it helps to have accurate definitions of the built-in messages.
24+
25+
:notes: [Utility Types](https://github.com/thinknathan/tsd-util-types/tree/main/types)
26+
include messages.
27+
28+
:boom: [Defold Annotations for Typescript](https://github.com/elMuso/defold-annotations-typescript/)
29+
can generate a definitions file with Defold's built-in messages.
30+
31+
# Working with Vector Math
32+
33+
TypeScript doesn't have a built-in way to understand the resulting type of
34+
an operation involving vectors.
35+
36+
```ts
37+
const result = go.get_position() + vmath.vector3(1, 1, 1); // type: number ?!??
38+
```
39+
40+
If you're confident about the result, you can cast the type:
41+
42+
```ts
43+
const result = (go.get_position() + vmath.vector3(1, 1, 1)) as vmath.vector3; // type: vmath.vector3
44+
```
45+
46+
Or you can use the
47+
[Operator Map Types](https://typescripttolua.github.io/docs/advanced/language-extensions#operator-map-types)
48+
provided by TSTL to enable full type checking.
49+
50+
:notes: [Utility Types](https://github.com/thinknathan/tsd-util-types/blob/main/types/vmath.d.ts)
51+
include all relevant vector math operations.
52+
53+
```ts
54+
namespace vmath {
55+
export const add: LuaAddition<vmath.vector3, vmath.vector3, vmath.vector3>;
56+
}
57+
const result = vmath.add(go.get_position(), vmath.vector3(1, 1, 1)) // type: vmath.vector3
58+
```

content/configuration/def-extensions.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Defold Extensions"
3-
order: 3
3+
order: 4
44
---
55

66
Defold is a modular engine, with a ton of functionality available through
@@ -24,5 +24,4 @@ and generate types automagically.
2424

2525
# Write Your Own
2626

27-
If all else fails, you can read the documentation and write your own types.
28-
Your preferred AI tool (like Chat-GPT) may also be able to help get you most of the way there.
27+
If all else fails, you can write your own types by hand, or ask an AI tool to help.

content/configuration/plugins.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,22 @@ order: 2
77

88
## Export Globals
99

10-
The [@ts-defold/tstl-export-as-global](https://github.com/ts-defold/tstl-export-as-global)
10+
The [tstl-export-to-global](https://github.com/thinknathan/tstl-export-to-global)
1111
plugin is used to allow the export function to emulate the expected behavior of
1212
"exports" in a Defold game script.
1313

1414
The `init`, `on_input`, `on_message`, `on_reload`, `update`, and `final` functions
1515
are executed on each game script by the Defold game engine. Each of those functions
16-
are expected to be defined in the file scope of the game script in order for the
17-
game engine to execute them.
16+
are expected to be defined in the file scope of the game script for the game engine
17+
to execute them.
1818

19-
In order to adhere to these requirements, the ***tstl-export-as-global*** plugin
20-
will look for any exported function that matches a `globals: { functions: [] }` array
21-
defined in the `.tsconfig.json` plugin configuration options, and hoist them to the global
22-
scope of the file.
19+
To adhere to these requirements, the ***tstl-export-to-global*** plugin will hoist
20+
all exports to the global scope of the file.
2321

2422
```json
2523
{
26-
"name": "@ts-defold/tstl-export-as-global",
27-
"match": ".*script.ts$",
28-
"globals": {
29-
"functions": [ "init", "on_input", "on_message", "on_reload", "update", "final" ]
30-
}
24+
"name": "tstl-export-to-global",
25+
"match": ".*\\..*script.ts$"
3126
}
3227
```
3328

content/configuration/ts-config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ These options are of particular concern:
3030
- `"luaTarget": "5.1"` - Defold recommends targeting Lua 5.1 for the broadest support
3131
of deployment targets.
3232
If you do not want to release your game on an HTML5 target, you may want to modify
33-
this to bring in [additional syntax features](https://typescripttolua.github.io/docs/caveats).
33+
the value to `"JIT"` to bring in [additional syntax features](https://typescripttolua.github.io/docs/caveats).
3434

3535
- `"luaLibImport": "require"` - This setting is used to generate the `lualib_bundle.lua`
3636
and then insert a require statement at the top of each script to bring in the

content/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ your [Defold](https://defold.com) game projects.
2323
- Typings and auto-complete for the [Defold API](https://defold.com/ref/stable/go/)
2424
- Project generator and project templates for a quick start
2525
- ESLint configured and ready to _lint_ your code
26+
- Prettier auto-formatting included
2627
- Type generator that stays up to date with the Defold _API_
2728
- Library of types for Defold native extensions and Lua libraries
2829
- Debugging with sourcemaps

0 commit comments

Comments
 (0)