Split Files
A long story makes a long js/script.js. You can split it into as many files as you like, one per chapter for instance, with nothing more than a text editor: each file adds its labels to the script, and index.html loads them in order.
Put a chapter in its own file
Create a file next to
js/script.js, for examplejs/chapter-2.js.In it, call
monogatari.script()with the labels it holds, written exactly as injs/script.js:monogatari.script({ 'Chapter2': [ 'y The second chapter starts in a file of its own.', 'end', ], });Jump to it from the first file as you would to any label:
monogatari.script({ 'Start': [ 'y Hello there.', 'jump Chapter2', ], });Load the new file in
index.html. Find the lines that load the game’s files, near the end of the<head>:<!-- Monogatari JavaScript Libraries --> <script src="./engine/core/monogatari.js"></script> <script src="./js/options.js"></script> <script src="./js/storage.js"></script> <script src="./js/script.js"></script> <script src="./js/main.js"></script>Add a line for your file after
js/script.js:<!-- Monogatari JavaScript Libraries --> <script src="./engine/core/monogatari.js"></script> <script src="./js/options.js"></script> <script src="./js/storage.js"></script> <script src="./js/script.js"></script> <script src="./js/chapter-2.js"></script> <script src="./js/main.js"></script>
Reload the page and play through: after “Hello there.”, the game jumps to the label in js/chapter-2.js.
The files load from top to bottom. Every monogatari.script() call adds its labels to the ones already there, and a label with the same name as an earlier one replaces it, so two files must not use the same label name.
If the name in index.html doesn’t match the file’s name, the browser skips the file and the game shows The label “Chapter2” does not exist when the story jumps there. Check the spelling, the folder and the .js ending.
TIP
If your game is on the web and should play offline, also add the file to the FILES list near the top of service-worker.js ('js/chapter-2.js', next to 'js/script.js',), so the service worker stores it on a player’s first visit. See Asset Preloading.
Split a game in several languages
In a game with MultiLanguage on (see Internationalization), the top level of the script holds languages instead of labels. One file per language works the same way as one file per chapter:
// js/script.js
monogatari.script({
'English': {
'Start': [
'Hi, welcome to the story.',
'end',
],
},
});// js/script-es.js
monogatari.script({
'Español': {
'Start': [
'Hola, bienvenido a la historia.',
'end',
],
},
});Splitting one language over several files needs another call. A second monogatari.script({ 'English': { … } }) replaces the whole English script, every label of the first file included, and the game then can’t find Start. Add labels to a language one at a time with monogatari.label() instead:
// js/chapter-2.js
monogatari.label('Chapter2', 'English', [
'Chapter two begins.',
'end',
]);
monogatari.label('Chapter2', 'Español', [
'Empieza el capítulo dos.',
'end',
]);monogatari.label() takes the label’s name, its language and its statements. It adds that one label (or replaces a label of the same name in that language) and leaves the language’s other labels as they were.
With a computer and Bun
This part is optional and only for games built with tools.
- The starter game’s web build (
build-web.ts, run with Bun; see Web) copies the wholejsfolder, so files you add there are included. A file you keep in another folder has to be added to theINCLUDElist inbuild-web.ts. - A game that uses Monogatari as an npm package, with a bundler, can split its script into modules that it imports instead of
<script>tags. See the Engine API for the package’s entry points.
Related pages
- Script and labels: what labels are and how the script moves between them.
- Jump: moving to a label in any file.
- Internationalization: the multi-language script.