You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+22-17
Original file line number
Diff line number
Diff line change
@@ -42,14 +42,14 @@ If you frequently need to update your story, pasting the content into `story.js`
42
42
Once the server is running, use the [other boilerplate](https://github.com/y-lohse/inkjs/blob/master/templates/browser_with_server) and place your story content inside `story.json`. Behind the scenes, the only difference is that we load the JSON file via ajax before creating the story:
43
43
44
44
```javascript
45
-
fetch("story.json")
46
-
.then(function (response) {
47
-
returnresponse.text();
48
-
})
49
-
.then(function (storyContent) {
50
-
story =newinkjs.Story(storyContent);
51
-
continueStory();
52
-
});
45
+
fetch('story.json')
46
+
.then(function (response) {
47
+
returnresponse.text();
48
+
})
49
+
.then(function (storyContent) {
50
+
story =newinkjs.Story(storyContent);
51
+
continueStory();
52
+
});
53
53
```
54
54
55
55
## Using node.js
@@ -65,14 +65,14 @@ Require the module: `var Story = require('inkjs').Story;`.
65
65
You can load the json file using a simple call to `require`:
66
66
67
67
```javascript
68
-
var json =require("./ink_file.json");
68
+
var json =require('./ink_file.json');
69
69
```
70
70
71
71
You can also load it using `fs`. In that case, please note that inklecate outputs a json file encoded **with** BOM, and node isn't very good at handling that.
72
72
73
73
```javascript
74
-
var fs =require("fs");
75
-
var json =fs.readFileSync("./ink_file.json", "UTF-8").replace(/^\uFEFF/, ""); //strips the BOM
74
+
var fs =require('fs');
75
+
var json =fs.readFileSync('./ink_file.json', 'UTF-8').replace(/^\uFEFF/, ''); //strips the BOM
76
76
```
77
77
78
78
### Starting a story
@@ -92,15 +92,15 @@ From there on, you can follow [the official documentation](https://github.com/in
92
92
93
93
There are a few very minor API differences between ink C# and inkjs:
94
94
95
-
### [Getting and setting ink variables](https://github.com/inkle/ink/blob/master/Documentation/RunningYourInk.md#settinggetting-ink-variables).
95
+
### [Getting and setting ink variables](https://github.com/inkle/ink/blob/master/Documentation/RunningYourInk.md#settinggetting-ink-variables)
96
96
97
97
On platforms that do not support [ES2015 Proxies](https://kangax.github.io/compat-table/es6/) (basically node.js v5, IE 11, Safari 9 and everything below), you can't directly read and write variables to the story state. Instead you will have to use the `$` function:
98
98
99
99
```javascript
100
-
_inkStory.variablesState.$("player_health", 100);
100
+
_inkStory.variablesState.$('player_health', 100);
101
101
//instead of _inkStory.variablesState["player_health"] = 100;
102
102
103
-
var health =_inkStory.variablesState.$("player_health");
103
+
var health =_inkStory.variablesState.$('player_health');
104
104
//instead of var health = _inkStory.variablesState["player_health"];
105
105
```
106
106
@@ -109,14 +109,14 @@ var health = _inkStory.variablesState.$("player_health");
109
109
`EvaluateFunction()` lets you evaluate an ink function from within your javascript. The "normal" call is the same than in C#:
110
110
111
111
```javascript
112
-
var result =EvaluateFunction("my_ink_function", ["arg1", "arg2"]);
112
+
var result =EvaluateFunction('my_ink_function', ['arg1', 'arg2']);
113
113
//result is the return value of my_ink_function("arg1", "arg2")
114
114
```
115
115
116
116
However, if you also wish to retrieve the text that `my_ink_function` output, you need to call it like this:
117
117
118
118
```javascript
119
-
var result =EvaluateFunction("my_ink_function", ["arg1", "arg2"], true);
119
+
var result =EvaluateFunction('my_ink_function', ['arg1', 'arg2'], true);
120
120
//now result is an object with two properties:
121
121
// result.returned is the return value of my_ink_function("arg1", "arg2")
122
122
// result.output is the text that was written to the output while the function was evaluated
@@ -125,6 +125,7 @@ var result = EvaluateFunction("my_ink_function", ["arg1", "arg2"], true);
Copy file name to clipboardexpand all lines: docs/compiler-differences.md
+3-2
Original file line number
Diff line number
Diff line change
@@ -5,15 +5,16 @@
5
5
The C# compiler is intented to always be used on a file system and thus the question of how files are included follow a classic pattern.
6
6
Nevertheless, when using the compiler inside a browser, the concept of "file" is a blurry one.
7
7
Inkjs provides 2 file handlers :
8
-
* A POSIX file handler : similar to the one used in the C# compiler that will look for files in folders
9
-
* A JSON file handler : expects a JSON object of the form
8
+
* A JSON file handler : it is included by default : it expects a JSON object representing all the files of the project of the form :
10
9
```
11
10
{
12
11
"filename1.ink": "INCLUDE filename2.ink",
13
12
"filename2.ink": "This content is included",
14
13
}
15
14
```
16
15
16
+
* A POSIX file handler : delivered as a separate `inkjs-full-posixhandler.js` file that must be included/required : similar to the one used in the C# compiler that will look for files in folders.
17
+
17
18
## Float and ints
18
19
19
20
As the JSON format and javascript in general do not differentiate between float and integers, the inkjs runtime is known to behave differently from the C# runtime when dealing with floating point operations.
If you are working in JavaScript, then that is all you need.
33
+
34
+
### TypeScript
35
+
36
+
If you are working in TypeScript then you will probably see an error message like:
37
+
38
+
> Cannot find module '../assets/myStory.ink' or its corresponding type declarations.
39
+
40
+
You need to declare your types for the `.ink` file. This is done using a [Declaration File](https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html).
41
+
42
+
Create one, such as `global.d.ts` in the top level of your source code, which reads like this:
43
+
44
+
```typescript
45
+
declaremodule'*.ink' {
46
+
const value:string;
47
+
exportdefaultvalue;
48
+
}
49
+
```
50
+
51
+
This says that when you import an `.ink` file, you will get a string.
52
+
53
+
This should work, but you might need to adjust your `tsconfig.json` file to tell it where to find `global.d.ts`:
54
+
55
+
```json
56
+
{
57
+
compiler: {
58
+
...
59
+
},
60
+
include: ["path/to/global.d.ts"]
61
+
}
62
+
```
63
+
64
+
Credit to [felixmosh's Stackoverflow answer](https://stackoverflow.com/a/66176397/19068) for providing my reference for this TypeScript.
0 commit comments