Skip to content

Scripting (JavaScript) ​

The Scripting plugin adds logic blocks that run JavaScript code and project functions that any block can use. It is for what the normal blocks do not cover well: loops, math, randomness with your own rules, lists, complex state… Not everyone needs it, so it is off by default: each project installs it separately.

The complete guide to what the code can do is the API reference.

Turning it on ​

  1. Open the Plugins section (side menu, Project group).
  2. Pick Scripting (JavaScript) in the list.
  3. Flip the On switch.

Scripting plugin settings: toggle activation and code example

Once on, the Scripting category (teal) appears in the block menu and in the legend of the node view. If you turn it off, the code blocks in the project are ignored (not deleted): Diagnostics warns you.

The plugin page also has:

  • Code blocks: every one in the project, each with its scene and script, its first line and its warnings (the ones with warnings come first). The button on the right takes you to the block.
  • Project functions: the function files.
  • Examples: what you write often (give an item if they have another, count attempts, a random prize…), ready to copy and paste into a block.

Step by step: your first code ​

  1. Turn the plugin on (above).

  2. In Logic, open or create a script, for example “On use Chest”. Add a step, search for “code” and pick Run the code.

  3. Click the step's box: the editor opens. Type game. and pick from the autocomplete; inside the quotes it offers the project names (scenes, items, characters…):

    js
    if (game.items.has("Key")) {
      game.items.add("Gold coin");
      await game.say("player", "At last!");
    } else {
      await game.say("player", "It's locked.");
    }

    Put await before anything that takes time (saying, walking, waiting, changing scene). If you get a name or the syntax wrong, the editor underlines it.

  4. Click Play and use the chest. If something fails, the Play bar warns you (“⚠ 1 error”) and the Debugger tells you the script and the line, with a button to go to the block.

  5. Going to repeat it elsewhere? Move it into a project function and call it with lib.openChest() or with the Call a function block.

  6. Check that the game can be finished: walkthroughs run the code like the game does.

The blocks ​

The code lives inside blocks, like any other step of a script: you see it in the node view, the human view and the text view, and you can copy, move, annotate and undo it.

BlockTypeWhat it does
Run the code …actionRuns the code and goes on to the next step when it finishes.
If the code holds …conditionEvaluates the code and follows the Yes or No branch depending on what it returns.
Call the function … with …actionCalls a project function with the values you give it, without writing code.

Script logic view displaying JavaScript code condition and execution blocks

Click the block’s box to open the code editor: monospaced, Tab indents, and below it you immediately see whether the syntax is correct. The API guide link opens this documentation.

Interactive JavaScript code popover editor with live syntax check

Run the code ​

js
game.vars.coins += 5;
await game.say("player", "Look, coins!");
if (game.vars.coins >= 20) {
  game.items.add("golden key");
}

If the code holds ​

Write an expression:

js
game.vars.coins >= 10 && game.items.has("key")

or several lines ending with return:

js
const total = game.items.count("coin") * 2;
return total > game.vars.price;

If the code fails (syntax error, a name that does not exist…), the condition counts as No and the error shows in the Play debugger with its line.

The code editor ​

Clicking the box of a code block opens a wide window with a real editor:

  • Colours, line numbers, folding and matching brackets. Tab indents; Esc closes the window (what you typed is already saved).
  • Find and replace with Ctrl+F.
  • Its own undo: Ctrl+Z undoes what you type, not the project's last change.
  • game. completion: typing game. lists every instruction, with what it takes and a line of help; the ones that take time (⏱) are written with await in front. Hovering an instruction shows its help.
  • Your project's names inside quotes: inside game.goto("…") you get your scenes; in game.items.has("…"), your items; in game.say("…"), your characters; in game.walkTo(who, "…"), what is in the script's scene; in game.sound("…"), your sounds… And after game.vars., your variables (the ones with spaces as game.vars["Hero name"]).
  • Errors while you type: a syntax error is underlined on its line, and the message shows below.
  • Snippets (button at the bottom, or at the start of a line): give an item if they have another, count attempts, a random prize without repeats, say and wait, only the first time, roll a die.
  • If you reload the page (F5) with the window open, it opens again on the same block.

If you prefer to write the code in another editor (VS Code…), verb-game.d.ts has the types of game so it can complete them.

Names are watched ​

The code names things by their name (game.goto("Cellar"), game.items.has("Key"), game.vars.gold), and the editor keeps track of it:

  • Diagnostics flags a name between quotes that does not exist, with its line (“Line 3: there is no scene “Cellar”.”), and takes you to the block. The code editor underlines it on the name itself. This matters: game.items.has("Kkey"), misspelled, gives no error when playing; it just says “no” every time.
  • “Where is it used”, the “Unused” filters and the warning when deleting count code too.
  • When you rename a scene, character, item, conversation, script or variable whose name appears in code, the editor asks whether to change it there too. With Change it too, the code is rewritten in the same step (Ctrl+Z undoes it all together).

Only what is written as is can be watched: a name the code computes (in a JavaScript variable or a template with ${…}) is not checked until the game is played.

Actions that take time ​

Instructions that take time (saying a line, waiting, walking, changing scene…) return a promise: put await in front so the code waits for them to finish before going on.

js
await game.say("player", "I'll take a look.");
await game.walkTo("player", "Door");
await game.wait(1);

Instant things (changing a variable, giving an item, showing a scene object) do not need await.

Everything that already exists is available too ​

The code does not have to reimplement anything: it can call any block of the editor.

js
// With the same sentence you would see in the human view (Spanish or English)
await game.do("Set the variable Gold to 7");
if (await game.check("If Gold > 5")) {
  await game.do("The player says: “I'm rich!”");
}

// Or by block type, with its values
await game.block("cam.shake", { seconds: 1 });

That way the code focuses on the logic while the blocks keep doing the cinema, sound and interface work. See game.do, game.check and game.block.

Recipes ​

A dice roll with your own rules

js
const roll = game.random(1, 6);
game.vars.lastRoll = roll;
if (roll === 6) {
  game.data.crits = (game.data.crits ?? 0) + 1;
}

A chest that gives a different item each time, without repeating

js
const prizes = ["coin", "key", "map"].filter((p) => !game.items.has(p));
if (prizes.length) {
  const prize = game.pick(prizes);
  game.items.add(prize);
  await game.narrate("You find: " + prize);
} else {
  await game.narrate("The chest is empty.");
}

Count how many times you visit a place (saved with the game)

js
game.data.visits = (game.data.visits ?? 0) + 1;
if (game.data.visits === 3) await game.say("player", "Here again…");

A condition with several rules

js
return game.visited("Basement") && game.items.count("battery") >= 2 && !game.items.has("broken flashlight");

A light sweep and a shake when a door opens

js
await game.light("#ff8844", 0.5);
await game.camera.shake(0.4);
await game.light("#ffffff", 1);

Reading the game ​

Besides variables and the inventory, the code reads the same things the condition blocks look at, without waiting for anything:

js
if (game.props.state("Chest") === "open" && !game.documents.has("Map")) {
  await game.say("player", "The map should have been in here…");
}
if (game.regions.has("Puddle", "Leo")) game.log("Leo is getting wet");
const left = game.countdown("bomb"); // seconds, or null if it is not running
  • Scene objects and hotspots: game.props.state("Chest") (the name of its state), game.props.visible("Chest") and game.hotspots.enabled("Door"). They are looked up in the current scene first, then in all.
  • Characters: game.characters.where("Leo") tells which scene they are in and where (null if in none).
  • Regions: game.regions.has("Puddle") checks whether the protagonist is inside (or the character you name), just like the block “If in the region”; game.regions.enabled("Puddle") tells whether it is enabled.
  • Countdowns, time of day, containers and documents: game.countdown("bomb"), game.timeOfDay, game.containers.has("Trunk", "Coin") (and .count, .list) and game.documents.has("Letter").

Write them as is (game.props.state(…)): the engine prepares these reads only for the code that names them. A computed access, such as game[name], gives an error that explains it. The full list is in the API reference.

Project functions ​

When the same code repeats in several blocks, write it once in a function file and call it wherever you need it. The files are on the plugin page (Plugins → Scripting → Project functions): New file creates one with an example function and From a template brings some ready-made ones (count attempts, swap one item for another, weighted random). Each file opens in the same editor as a block's code, with its colors, autocomplete and warnings.

js
// If the player carries "give", take it and give them "get".
async function swap(give, get, line) {
  if (!game.items.has(give)) return false;
  game.items.remove(give);
  game.items.add(get);
  if (line) await game.say("player", line);
  return true;
}

From any code block, the functions are in lib:

js
if (await lib.swap("Rusty key", "Clean key", "Now it shines.")) {
  game.vars.clean += 1;
}
  • Every function name(…) { … } written at the top level of the file counts (not the ones inside another function, nor arrow functions stored in a variable). If the function waits for something (await), write it as async function and call it with await.
  • When you type lib., the editor offers the project functions with their parameters. A // comment right above a function shows as its help.
  • Inside a file you use game just like in a block, and a function can call another one (also from another file) with lib.other(…).
  • The Call the function … with … block does the same without writing code: pick the function and give each parameter a value. A number or true / false go as they are; anything else, as text. So someone who does not code can use what someone else wrote.
  • The names a file writes are watched like a block's (Diagnostics, “Where it is used”, renaming). Diagnostics also warns about a file with a syntax error, a function with the same name as another one (the first one counts) and a lib.something that does not exist.
  • Deleting a file tells you how many blocks call its functions; Ctrl+Z brings it back.
  • The files travel with the game and run in the same sandbox as the blocks' code. They are only loaded in the blocks that use lib: an error in a file does not break the other blocks.

Errors and console in Play ​

When you test the game with Play, everything that happens in the code shows in Debugger → Log:

  • An error says which script it comes from and which line of the code it is on: “Code · “Entering the cellar” · line 3: Cannot find scene “Cellar”.”. Next to it there is a button, Go to the block, that closes Play and leaves you in Logic with that block selected.
  • What you write with console.log(…) or game.log(…) shows under the Code filter, also with its line (at most 200 messages per block, so a loop does not fill the log). It still goes to the browser console too (F12), which is the only place in an exported game.
  • If there are errors you have not looked at yet, a red button with how many there are appears in the Play bar (“⚠ 2 errors”): click it to open the Log.
  • A call without await that fails counts as an error too, even on the last line of the code. For example, if you write game.items.add("Kkey") (a name that does not exist) at the end of an “If the code holds” block, the condition counts as No and the Log tells you the line. The same goes for something that takes time (game.say, game.walkTo…) without await, if it fails before the code ends; if it fails afterwards, the code has already finished and it does not show.
  • An error inside a project function says the file and its line (“Code · functions “Inventory” · line 4: …”), and the button takes you to that file.
  • A syntax error is flagged while you type, below the editor, and in Diagnostics; in Play it shows without a line.
  • If the code gets stuck in an infinite loop, it is stopped after five seconds: the error says the script, but not the line.

To look at the code calmly, put a breakpoint on the code block (Debugger → Logic): the script stops before running it and you can see the variables in the State tab.

Walkthroughs with code ​

Walkthroughs and Find solution run the code like the game does: sandboxed, with the project functions and with “Call a function”. That way you can check that an adventure with code can be finished.

  • Randomness is the simulator's: if the code rolls dice (game.random, Math.random), Find solution tries both outcomes, as with the random blocks.
  • The simulator does not walk: for the code nobody is inside any region, characters stay where they started and countdowns do not tick. Check whatever depends on that by playing in Play.
  • A code error shows among the warnings, with its script (or its function file) and its line. Code that hangs is cut after a second and a half and is not run again in that test.

Safety and limits ​

  • The code runs isolated in a separate process (a Web Worker): it cannot see the page or the engine, and has no access to the internet, cookies or browser storage. It can only ask for what game offers.
  • This protects whoever plays someone else’s project: opening a .verbproj or a published adventure cannot take anything from your computer.
  • If the code gets stuck in an infinite loop, the engine stops it after a few seconds and the game goes on.
  • Each block runs on its own: local variables (let, const) are not kept from one block to the next. To keep something between blocks use a project variable or game.data, which is also saved with the game.
  • Code blocks do not show up in the Puzzle map (what they do is unknown until they run): if a puzzle depends on code, add a note to the script.

Writing code from the text view ​

In the Text view (and when importing/exporting) a code block is one line with the code in double quotes, written as a JSON string (line breaks are \n):

text
Run the code "game.vars.coins += 5;\nawait game.say('player', 'Coins!');".
If the code holds "game.items.has('key')":
    The player says: “I have the key.”

With the AI agent ​

If you also have the AI Agent (MCP) plugin, the agent can see which plugins exist and turn Scripting on or off when it needs to (list_plugins, set_plugin), and can read the API guide (get_scripting_api) before writing code. While it plays in Play (play_game), it reads the code errors and console with debug_log. Before writing code, it checks it with check_code (the syntax and the names that do not exist, with their line), and rename_entity also changes the code that names what it renames. It reads and writes the project functions with read_code_files and write_code_files, and uses the “Call a function” block (scr.call). The agent plugin itself cannot be turned off by the agent: only you can.

Related tutorials ​

On the editor's home screen, Tutorials (all of them have the plugin turned on):

  • Your first code block: The two code blocks: “Run the code” (subtract coins, give an item and speak with await) and “If the code holds” (a condition on the game state).
  • Scenes, camera and blocks from code: A cinematic written in a single code block and how to call the editor’s blocks from code with game.do, game.check and game.block.
  • Randomness, data and lists: A chest with a random prize, a die and a counter in game.data; a three-rune lock with a list and every(), and a loop that goes through the backpack.