Script editor
The game has a code editor built in. It edits the files in your mods folder while the game runs, reloads a mod the moment you save, runs Lua inside a running mod, and shows each mod's errors and log next to the code.
Opening it
Press F1 (or the backquote key, F3 or F12) to show the developer tools bar, open its Mods menu and choose Script editor. Typing /tools scripts in the developer console does the same.
The window has four parts:
- the explorer on the left, with every mod in your mods folder;
- the editor in the middle, one tab per open file, with Run, the Client / Server switch and a reload button above it;
- the panel below it, with the Problems, Output, Console and Log tabs;
- the status bar at the bottom: the current file's mod, Hot reload, messages, line and column, language, line ending, indentation and Shortcuts.
Drag the edges between the parts to resize them. Ctrl+B hides or shows the explorer, Ctrl+J the panel.
The explorer
Each mod is a top-level entry with a dot for its state: green running, red faulted, blocked or invalid, amber waiting for your consent, grey off. Click a mod or a folder to expand or collapse it, and a file to open it in a tab. The box at the top filters files by name.
The buttons above the list make a new file, a new folder or a new mod, look again for new, removed or changed mods, and collapse every folder. Right-click a row for everything else:
| Right-click on | You get |
|---|---|
| a mod | New file…, New folder…, Reload mod, Open mod.json |
| a folder | New file…, New folder…, Rename…, Duplicate |
| a file | Open, Rename…, Duplicate |
| any row | Show in folder, Copy path, Copy relative path, Delete… |
| empty space | New mod…, Refresh mods, Open the mods folder |
- New mod asks for a template, an id (
yourname.modname) and a name. The mod is created in your mods folder and its first script opens. - New file creates the file in the selected mod or folder. Type
scripts/tool.luato create the folder as well. A file has to be inside a mod: a file outside one never runs. - Delete asks first and cannot be undone. A whole mod can only be deleted when it is in your own mods folder.
- Drag a file or folder onto another folder to move it.
- In the Windows game you can drag files from your desktop onto a mod to copy them in. A dropped folder that has a
mod.jsonbecomes a new mod in your mods folder.
Files whose names start with a dot are not listed.
Tabs and files
A dot on a tab marks unsaved changes. Middle-click a tab to close it, or right-click it for Close others, Close saved, Show in explorer and Show in folder. Closing a tab with unsaved changes asks first.
| File | Opens as |
|---|---|
.lua, .json, .uxml, .uss, .txt, .md, .csv and other text | Code. Lua, JSON, layout and style files are coloured. |
.png, .jpg, .jpeg | A picture, with Fit and 100% |
| anything else (models, sounds, video), or a file that holds binary data | A card with its type and size, Show in folder and Open with system app |
Big files are read in the background, so opening one does not stall the game. Text files follow these rules:
| Text file | What happens |
|---|---|
| up to 2 MB | Opens normally |
| over 2 MB | Opens without colours |
| over 4 MB | Opens read-only, without colours |
| over 32 MB | Not opened: you get the card instead |
Files keep their line endings and an existing byte order mark when saved. Tabs in a file become spaces when it opens; the status bar shows the indentation it found, and Tab inserts that many spaces. Enter keeps the indentation, and indents one step more after then, do, function(…) or an open bracket. Typing end, else, until or a closing bracket alone on a line steps it back.
Saving and hot reload
Ctrl+S saves the file and Ctrl+Shift+S saves every tab. While the status bar says Hot reload: On (the default; click it to switch), saving a file of a mod that is switched on reloads that mod straight away: it stops, everything it made is removed, and it starts again with your change. The status bar says how it went: ✓ my-mod reloaded in 14 ms, or the first error with its file and line, which also goes to Problems.
- A mod that faulted is reloaded too, so fixing the error and saving starts it again.
- A mod that is off, or waiting for your consent, is not reloaded. The status bar says so; switch it on, or allow it, in Mods.
- Reloading is refused while other players are connected, because their games would run different code. The file is still saved.
F5, or the reload button next to Run, reloads the current file's mod from what is on disk, without saving.
Editing in another editor
You can keep using your own code editor. While the script editor is open, it watches the mods folder:
- a file open in a tab without unsaved changes reloads quietly, and you keep your place;
- a file you changed in both places gets a bar above the code: Load disk version, Keep mine, or Open disk version to compare the two side by side (read-only);
- a file deleted on disk is marked, and saving writes it again;
- with hot reload on, saving in the other editor reloads the mod, once per save.
Running code
Ctrl+Enter, or Run, runs the selected lines inside the mod the file belongs to, or the whole file when nothing is selected. The code runs with that mod's globals, permissions and time budget. The Client / Server switch next to Run picks which copy of the mod runs it: your own client copy, or the server copy.
The Output tab shows what the code logged, then => and the value of the last expression. An error goes to Problems with the line number of the file, even when you ran a selection from the middle of it.
- Running code is for the host and single player. A player who joined someone else's game has no server copy on their machine, so the editor refuses.
- The mod must be running. Its server copy only exists while a session runs (single player counts), so in the main menu, run on Client.
- Up to 64 KB of code runs at a time.
- Running a whole entry script runs it a second time inside the mod, so it may make a second copy of its panels. To start a script afresh, save it and let it reload.
The Console
The Console tab is a Lua prompt inside a running mod. Choose the mod and Client or Server in the tab's header, type, and press Enter. Shift+Enter starts a new line, and Up and Down step through what you ran before. Results print as => value, errors in red.
mod.side
storage.keys()
mod.settings.all()As with /lua in the developer console, the code runs inside the mod with its permissions and time budget. It is a way to look into your mod while it runs, not a way around the sandbox.
Problems and error markers
The Problems tab collects the errors and warnings of saving, running and mods that stopped, each with its file and line. Click one to open the file at that line. A mod that faults while you work shows up here at once, and the status bar names the error.
In the editor, a line with a problem is tinted and gets a marker by its line number. Point at the line number to read the message. A mod's problems clear when it reloads cleanly.
The Log
The Log tab follows the mod log: what mods write with log.info, print and the rest, and the errors the game reports for them. Select a mod in the explorer to see only its lines. All, Info, Warnings and Errors filter by level, the box filters by text, and Follow keeps the newest line in view. A file:line in the log is a link to that place.
The Output tab collects what saving, reloading and running report, with a Clear button.
Finding your way
- Ctrl+P opens any file in any mod by typing part of its name.
- Ctrl+F finds in the current file and Ctrl+H replaces, with match case, whole word and regular expressions. Enter and Shift+Enter step through the matches. All replaces every match in one undo step.
- Ctrl+G goes to a line. Clicking the line and column in the status bar does the same.
- In Lua files, typing a module name and a dot (
ui.), or Ctrl+Space, lists what you can call there, from the API of the game you are running. Completions need mods to be running. - Ctrl+=, Ctrl+- and Ctrl+0 zoom the code in, out and back. Ctrl with the mouse wheel zooms too.
Shortcuts
Ctrl+Shift+/, or Shortcuts in the status bar, shows this list in the game.
| Files | |
|---|---|
| Save (reloads the mod) | Ctrl+S |
| Save all | Ctrl+Shift+S |
| New file | Ctrl+N |
| Quick open | Ctrl+P |
| Close tab | Ctrl+W |
| Next tab | Ctrl+Tab or Ctrl+PgDn |
| Previous tab | Ctrl+Shift+Tab or Ctrl+PgUp |
| Run | |
|---|---|
| Run selection (or file) | Ctrl+Enter |
| Reload mod | F5 |
| Console: history | Up / Down |
| Search and navigate | |
|---|---|
| Find | Ctrl+F |
| Replace | Ctrl+H |
| Next / previous match | Enter / Shift+Enter |
| Go to line | Ctrl+G |
| Word left / right | Ctrl+Left / Right |
| Line start (first character, then column 1) / end | Home / End |
| File start / end | Ctrl+Home / End |
| View | |
|---|---|
| Toggle explorer | Ctrl+B |
| Toggle bottom panel | Ctrl+J |
| Zoom in / out / reset | Ctrl+= / - / 0 |
| Shortcut list | Ctrl+Shift+/ |
| Editing | |
|---|---|
| Undo / redo | Ctrl+Z / Ctrl+Y |
| Cut / copy / paste (the whole line when nothing is selected) | Ctrl+X / C / V |
| Duplicate line or selection | Ctrl+D |
| Delete line | Ctrl+Shift+K |
| Move line up / down | Alt+Up / Down |
| Toggle comment | Ctrl+/ |
| Indent / outdent | Tab / Shift+Tab |
| Select all | Ctrl+A |
| Completions | Ctrl+Space |
| Explorer | |
|---|---|
| Open | Enter |
| Rename | F2 |
| Delete | Del |
| Collapse / expand | Left / Right |
F1, F3 and F12 always show or hide the developer tools, even while you type. The backquote key does too, except while you are typing in the editor, where it types a backquote.