Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

API reference: the vb module

The vb module is the link between a script and the game. This page lists every export, grouped by task.

Using the module

import { on, log, drawText } from 'vb';   // named imports
import vb from 'vb';                      // default import: one object with everything
vb.log('hello');
const vb = require('vb');                 // CommonJS style

A global object named vb with the same functions also exists, so short scripts need no import at all.

In TypeScript files use the first or second form. import * as vb from 'vb' does not work there. See TypeScript support and its limits.

The module exports functions and three classes. It exports no constants. Use define() to look up the game’s named constants, such as tile numbers.

Availability

Functions marked Duke work only in the Duke runtime games: Duke Nukem 3D, World Tour, Ion Fury, NAM, NAPALM and WWII GI. In Shadow Warrior these functions exist but throw ReferenceError: This vb API is not supported by the active game.

Only on, off, log and breakpoint work in every game that runs scripts.

General rules

  • Numbers are integers. Every numeric argument is converted to a 32-bit integer. Fractions are cut off. A value that is not a number becomes 0.
  • Unknown names give undefined. A getter returns undefined when the member, gamevar or constant name does not exist. A setter with an unknown name does nothing.
  • Indices are not checked. getActor, getSector, getWall, getPlayer and the other struct functions pass your index straight to the engine. An index that is out of range, such as -1, reads or writes memory that does not belong to the object. The game can crash. Check an index before you use it.
  • Too few arguments throw. Most functions throw a TypeError when a required argument is missing.
  • Changes are local. A script changes the game only on the computer it runs on. See Keeping the game in sync.

Logging

FunctionReturnsDescription
log(...values)undefinedWrites one line to the game log and console, prefixed with [js].
  • Each value is converted to a string. The values are joined with single spaces.
  • Objects print as [object Object]. Use JSON.stringify(value) to see their content.
  • A line is cut off after 1023 characters.
  • console.log, console.info, console.warn and console.error are the same function.

Events

FunctionReturnsDescription
on(name, handler)undefinedCalls handler every time the event name fires.
off(name)undefinedRemoves all handlers of the event name.
  • name is a string. The event names are listed in Events.
  • handler is a function. Most events pass one event object. Some pass nothing.
  • You can register several handlers for one event. They run in the order you registered them.
  • For CON events the EVENT_ prefix is optional and the name is not case-sensitive. on('jump', …) and on('EVENT_JUMP', …) are the same.
  • on throws a TypeError if handler is not a function.
  • off cannot remove a single handler. It also removes handlers that other mods and the component system registered for that event. Avoid off('frame') and off('actorDestroyed') if you use components.
import { on, log } from 'vb';

on('actorSpawned', (e) => {
  log(`sprite ${e.sprite} spawned with tile ${e.tile}`);
});

HUD text

Duke.

FunctionReturnsDescription
drawText(id, x, y, text, pal?, shade?)undefinedShows a line of text on the screen, or replaces the line with the same id.
clearText(id?)undefinedRemoves the line with this id. Without an argument it removes all lines.
  • id: any integer you choose. It names the line. Calling drawText again with the same id replaces the text.
  • x, y: position of the text in a 320 × 200 screen. 0, 0 is the top-left corner.
  • text: the string to show. At most 191 characters are kept.
  • pal: palette number that colors the text. Default 0.
  • shade: brightness. 0 is normal, negative values are brighter, positive values are darker. Default 0.

A line stays on screen until you clear or replace it. You do not need to draw it again every frame. Lines are shown during play and on the title and menu screens.

There are 64 lines in total, shared by all loaded scripts. When all 64 are in use, a new id is ignored. Pick ids that other mods are unlikely to use.

Spawning and actors

Duke.

FunctionReturnsDescription
spawn(tile, x?, y?, z?, owner?)sprite index, or -1Creates an actor of tile tile and initializes it like a normal game actor.
A_Spawn(spriteNum, tileNum)sprite indexThe engine’s spawn routine. Creates an actor of tile tileNum at the position of the existing sprite spriteNum, which becomes its owner.
A_InsertSprite(whatsect, s_x, s_y, s_z, s_pn, s_s, s_xr, s_yr, s_a, s_ve, s_zv, s_ow, s_ss)sprite indexThe engine’s low-level insert. Creates a raw sprite without running actor setup.
getActor(sprite, member, parm2?)number, or undefinedReads a member of a sprite.
setActor(sprite, member, value, parm2?)undefinedWrites a member of a sprite.
shoot(sprite, tile)numberMakes the sprite fire the projectile or weapon with tile tile. Returns the engine’s result, or -1 if sprite is out of range.
getProjectile(tile, member)number, or undefinedReads a setting of the projectile type defined for tile.
setProjectile(tile, member, value)undefinedWrites a setting of the projectile type defined for tile.
getActiveProjectile(sprite, member)number, or undefinedReads a setting of one flying projectile sprite.
setActiveProjectile(sprite, member, value)undefinedWrites a setting of one flying projectile sprite.
getTileData(tile, member)number, or undefinedReads data of a tile, such as its size.
setTileData(tile, member, value)undefinedWrites data of a tile.

spawn

  • tile: tile number. An invalid number returns -1.
  • x, y, z: map position. If all three are 0 or missing, the actor appears at the local player’s position.
  • owner: optional sprite index. Its sector is used as the starting point to find the sector of x, y. Without it the local player’s sector is used.
  • Returns -1 when no sector can be found, for example when no game is running.
  • The new actor fires actorSpawned.

In multiplayer do not call spawn. Use the synchronized console command instead: run('spawn <tile> <player>'). See Server scripts.

A_Spawn and A_InsertSprite

These are the engine’s own functions with their original names and argument order. They are for people who know the engine.

  • A_Spawn(spriteNum, tileNum): when spriteNum is -1, the second argument is not a tile. It is the index of an existing sprite that is then set up as an actor.
  • A_InsertSprite arguments, in order: sector, x, y, z, tile, shade, x-repeat, y-repeat, angle, velocity, z-velocity, owner sprite, status list number. All 13 are required.
  • If the map has no free sprite left, the game exits.

Actor members

member is the name CON uses for the field. Names are not case-sensitive. Common ones:

x, y, z, ang, sectnum, statnum, picnum (tile), cstat, shade, pal, xrepeat, yrepeat, xvel, yvel, zvel, owner, lotag, hitag, extra (health for most actors)

parm2 is an extra index that a few members need. Most members ignore it.

Tile data members: xsize, ysize, animframes, xoffset, yoffset, animspeed, animtype, gameflags.

Players

Duke.

FunctionReturnsDescription
player(){ sprite, x, y, z }The local player’s sprite index and position. sprite is -1 and the position is 0 when there is no player.
getPlayer(player, member, parm2?)number, or undefinedReads a member of a player.
setPlayer(player, member, value, parm2?)undefinedWrites a member of a player.
getInput(player, member)number, or undefinedReads the player’s current input.
setInput(player, member, value)undefinedWrites the player’s current input.
addAmmo(player, weapon, amount)undefinedAdds ammo for weapon number weapon, up to the weapon’s maximum.
addWeapon(player, weapon, switchTo?)undefinedGives the weapon. switchTo defaults to 1: the player switches to it. Pass 0 to keep the current weapon.
  • player is the player index. In a single-player game it is 0.
  • addAmmo and addWeapon do nothing when the player index is invalid. They do not check the weapon number.
  • Common player members: posx, posy, posz, ang, cursectnum, i (the player’s sprite index), curr_weapon, ammo_amount (needs parm2 = weapon number), gotweapon (needs parm2 = weapon number), on_ground, dead_flag, max_player_health, timebeforeexit.
  • Input members: avel, q16avel, horz, q16horz, fvel, svel, bits, extbits.
import { getPlayer, player, getActor } from 'vb';

const me = player();
if (me.sprite >= 0) {
  const health = getActor(me.sprite, 'extra');
  const ammoOfWeapon1 = getPlayer(0, 'ammo_amount', 1);
}

Sectors and walls

Duke.

FunctionReturnsDescription
getSector(sector, member)number, or undefinedReads a member of a sector.
setSector(sector, member, value)undefinedWrites a member of a sector.
getWall(wall, member)number, or undefinedReads a member of a wall.
setWall(wall, member, value)undefinedWrites a member of a wall.

Common sector members: ceilingz, floorz, ceilingpicnum, floorpicnum, ceilingshade, floorshade, ceilingpal, floorpal, ceilingstat, floorstat, visibility, lotag, hitag, extra, wallptr, wallnum.

Common wall members: x, y, point2, nextwall, nextsector, cstat, picnum, overpicnum, shade, pal, xrepeat, yrepeat, xpanning, ypanning, lotag, hitag, extra.

Heights (z values) get smaller as you go up.

Gamevars and CON data

Duke.

FunctionReturnsDescription
getGamevar(name, sprite?, player?)number, or undefinedReads a CON gamevar. undefined if no gamevar has this name.
setGamevar(name, value, sprite?, player?)undefinedWrites a CON gamevar. Does nothing if no gamevar has this name.
define(name)number, or undefinedReturns the value of a CON constant created with define, such as a tile name.
getUserdef(member, parm2?)number, or undefinedReads a member of the game’s global settings structure (userdef in CON).
setUserdef(member, value, parm2?)undefinedWrites a member of the game’s global settings structure.
  • Gamevar names and define names are case-sensitive.
  • sprite and player select which copy to use for per-actor and per-player gamevars. For global gamevars leave them out. Both default to -1.
  • getUserdef and setUserdef take the member name first. They have no index argument.
  • Common userdef members: god, multimode, level_number, volume_number, player_skill, eog, pause_on.
import { define, getGamevar, setGamevar, getUserdef } from 'vb';

const trooper = define('LIZTROOP');             // a tile number in Duke Nukem 3D
const gravity = getGamevar('gravitationalconstant');
setGamevar('gravitationalconstant', 88);
const players = getUserdef('multimode');

Sounds and weapons

Duke.

FunctionReturnsDescription
sound(num, sprite?)undefinedPlays sound number num. With a valid sprite index the sound comes from that sprite. Without it the sound is not positioned.
shoot(sprite, tile)numberSee Spawning and actors.
addAmmo(player, weapon, amount)undefinedSee Players.
addWeapon(player, weapon, switchTo?)undefinedSee Players.

Sound numbers are the ones the game’s CON files define. If the CON files give a sound a name, define('<NAME>') returns its number.

Duke.

FunctionReturnsDescription
setMenuText(original, replacement)undefinedWhenever the menu draws a string that is exactly original, it draws replacement instead.
setMenuBackground(tile)undefinedUses tile tile as the full-screen picture behind the menu when no game is shown. Pass null to restore the default.
currentMenu()numberThe id of the menu page that is current.
openMenu(menuId)undefinedSwitches the menu system to the page menuId.
setMenuRoute(menuId, itemIndex, destination)undefinedRedirects a menu item to another page.
menuSelection()numberThe index of the highlighted menu item.
setMenuSelection(index)undefinedHighlights the menu item with this index.
  • Menu ids are the game’s internal page numbers. Page 0 is the main menu.
  • setMenuText affects text drawn with the game’s large menu font. Replacements stay until the game exits.
  • setMenuBackground ignores tiles that do not exist. A CON handler for EVENT_GETMENUTILE can still override your tile.
  • setMenuRoute(menuId, itemIndex, destination): when the player is on page menuId, has item itemIndex highlighted, and the game changes page, the game goes to destination instead.
    • itemIndex -1 matches every item of that page that has no route of its own.
    • destination -1 keeps the current page. -2 closes the menu.
    • destination null removes the route.
  • Use the menu events to learn page ids: log e.menu from a menuChanged handler.

Session and multiplayer

Duke.

FunctionReturnsDescription
session()objectA snapshot of the current game session.

The returned object is a copy. It does not change later. Call session() again for fresh values.

FieldTypeMeaning
startedbooleantrue while a level is being played.
skillnumberSkill level, starting at 1.
episodenumberEpisode number, starting at 1.
levelnumberLevel number inside the episode, starting at 1.
episodeNamestring or undefinedName of the episode.
levelNamestring or undefinedName of the level.
userMapstring or undefinedFile name of the user map, when one is loaded.
localPlayernumberIndex of the player on this computer, or -1.
hostPlayernumber or undefinedIndex of the hosting player.
hostNamestring or undefinedName of the hosting player.
serverAddressstring or undefinedOn a client that joined through an address: that address. undefined on the host.
playersarrayOne entry per player slot.

Each entry of players:

FieldTypeMeaning
indexnumberPlayer index. Use it with getPlayer, addAmmo and similar functions.
namestringPlayer name.
spritenumberThe player’s sprite index, or -1.
isLocalbooleantrue for the player on this computer.
isHostbooleantrue for the hosting player.

Chat and server commands

FunctionReturnsDescription
sendMessage(text)undefinedDuke. Sends a chat message.
registerCommand(name, handler)undefinedDuke, server only. Registers a !name chat command.

sendMessage

  • On the host of a multiplayer game the message goes to every player and is shown as Server: <text>.
  • Otherwise it is sent as a normal chat line from the local player: <name>: <text>.
  • An empty string is ignored.
  • The message does not fire chatMessage on the computer that calls sendMessage. The other computers receive it as a chatMessage.
  • Be careful with a handler that answers every chatMessage with sendMessage. If two computers run such a script, they keep answering each other.

registerCommand

  • It throws a TypeError unless this computer is hosting a multiplayer game in master/slave mode. It throws in single-player games and on clients.
  • name may start with !. It may contain only letters, digits, _ and -, at most 32 characters. It is not case-sensitive.
  • Registering the same name again replaces the earlier handler.
  • When a player types !name some text in chat, the handler receives one object. vb.d.ts calls its type CommandRequest:
FieldTypeMeaning
playernumberIndex of the player who sent the command.
commandstringThe command name, in lower case, without !.
argsstringEverything after the command name, with leading spaces removed. One string, not split.
reply(text)functionSends text to that player only.
  • If the handler returns a string, the string is sent to that player, like reply.

See Server scripts for details and a complete example.

Console

FunctionReturnsDescription
run(command)undefinedDuke. Runs a console command, for example run('map e1l2').
  • A leading / is allowed and ignored.
  • The command does not run immediately. It is put in a queue and runs after your code returns, the next time the game loop passes. This works during play and at the title menu.
  • You cannot read a command’s output from the script.
  • run is the same as cvar.run from the cvar module.

Debugging

FunctionReturnsDescription
breakpoint()undefinedWhen DevTools is enabled, pauses script activity after the current function returns. Does nothing otherwise.

See Debugging with VS Code.

Components, nodes and prefabs

Duke. These exports are explained in Components. This is the summary.

ExportReturnsDescription
VBComponentclassBase class for your components.
VBNodeclassAn empty object that can hold components. new VBNode() creates one.
VBPrefabclassA tile plus a list of component classes. Create it with createPrefab.
attach(entity, component)the componentAttaches a component to a sprite index or a VBNode. component is a class or an instance.
detach(entity, component)booleanRemoves one component. true if it was attached.
getComponent(entity, type)component, or undefinedThe first attached component that is an instance of type.
getComponents(entity, type?)arrayAll attached components, or only those of type.
setComponentEnabled(component, enabled)undefinedTurns a component on or off. Same as assigning component.enabled.
createNode(...components)VBNodeCreates a node and attaches the given components.
destroyNode(node)booleanDestroys a node and all its components. false if it was already destroyed.
createPrefab(tile, ...components)VBPrefabDescribes an actor tile together with component classes.
spawnPrefab(prefab, x?, y?, z?, owner?)sprite index, or -1Calls spawn for the prefab’s tile and attaches its components to the new sprite.

Alphabetical list

All 58 exports:

A_InsertSprite, A_Spawn, addAmmo, addWeapon, attach, breakpoint, clearText, createNode, createPrefab, currentMenu, define, destroyNode, detach, drawText, getActiveProjectile, getActor, getComponent, getComponents, getGamevar, getInput, getPlayer, getProjectile, getSector, getTileData, getUserdef, getWall, log, menuSelection, off, on, openMenu, player, registerCommand, run, sendMessage, session, setActiveProjectile, setActor, setComponentEnabled, setGamevar, setInput, setMenuBackground, setMenuRoute, setMenuSelection, setMenuText, setPlayer, setProjectile, setSector, setTileData, setUserdef, setWall, shoot, sound, spawn, spawnPrefab, VBComponent, VBNode, VBPrefab