Sharing Your Extensions
Monogatari 2 has no plugin system: there’s no call that installs a package of actions and components. An extension you share is a set of files, usually one .js file with its classes and their registration and one .css file with its styles, that a game copies into its folder and loads from index.html. Nothing to install, so it works for anyone who can edit their game’s files.
Package an extension
This packages the Location action to share. Put its code in a file of its own, location.js, inside a pair of braces:
/* global Monogatari, monogatari */
// Location badge for Monogatari 2.9.0
// 'location Palacio de Bellas Artes' shows a badge with the place's name,
// 'location' on its own takes it away.
{
class ShowLocation extends Monogatari.Action {
// The whole class from Going Back and Saving
}
monogatari.registerAction(ShowLocation);
}And its styles in location.css:
.location-badge {
position: absolute;
top: 1rem;
left: 1rem;
z-index: 11;
padding: 0.5rem 1rem;
border-radius: 0.5rem;
background: var(--main-color);
color: #ffffff;
}The braces make a block: a class declared inside it is only known inside it. Without them, ShowLocation would be a name shared by every script the game loads, and a game that already has something called ShowLocation would stop with “Identifier ‘ShowLocation’ has already been declared”.
Load it in a game
Copy location.js into the game’s js folder and location.css into its style folder. Then load them from index.html: the script after js/script.js and before js/main.js, the stylesheet after style/main.css:
<link rel="stylesheet" href="./engine/core/monogatari.css">
<link rel="stylesheet" href="./style/main.css">
<link rel="stylesheet" href="./style/location.css"><script src="./js/script.js"></script>
<script src="./js/location.js"></script>
<script src="./js/main.js"></script>The script must come after engine/core/monogatari.js, which defines Monogatari and monogatari, and before js/main.js, which calls monogatari.init(): an action registered after that never gets its setup(). The game can use location statements in its script right away.
Write it to be shared
- Name everything after the extension. Its action ids, its keys in
monogatari.state(), its histories, its CSS classes, its interface string keys and its error dialog ids all share one game with everyone else’s.location-badgeand'location'are fine for one game; a shared extension is safer with a prefix of its own. - Use the engine’s names, not your own globals. Reach the engine as
monogatariand the base classes asMonogatari.ActionandMonogatari.Component, which every game has. - Don’t declare names the browser already has.
Location,ImageandTextare names every page already has (window.Locationand the others). Inside a block your class can’t clash with a game’s, but it hides the browser’s own for the rest of the block. - Give English strings for your interface. Call
monogatari.translation('English', { … })for the keys your components use, so the game works before its author translates them. See Translating the Interface. - Say which version you wrote it for. Extensions use the engine’s methods and its markup, which can change between versions.
monogatari.versionis'2.9.0'in this version of Monogatari. - Document it for the person writing the script: the statements and what each word does, the variables it reads from
js/storage.js, the CSS classes to restyle it, the string keys to translate.
Related
- Where your code goes: the order
index.htmlloads scripts in. - Actions and Components: what you’re packaging.
- With a Bundler: an extension for games built with Node.js or Bun.