Triggers
MUD Client triggers explained: Contains, Regex and Variable updated, multi-line patterns, examples to copy, and a checklist for a trigger that will not fire.
A trigger watches the text the game sends and runs an action when a line matches. Triggers answer greetings, eat when you are hungry, file tells into the Messages panel, hide spam, colour your enemies' names and warn you when your health is low. This guide covers how a trigger matches, a set of examples to copy, and what to check when one does not fire. It works the same on macOS and Windows.
Making a trigger
Open the Scripting window with the Aliases button in the toolbar (on Windows also Tools > Scripting), switch to the Triggers tab and click +. A trigger has:
- Trigger name - shown in the list, and what
#enable,#disableandsuspendTriggersuse to find it. Put triggers in folders to switch a whole group at once. - Match type - Contains, Regex or Variable updated (explained below).
- Pattern - the text or regular expression to look for.
- Match from start of line - only match at the beginning of a line. A prompt such as
>in front of the line still counts as the start. - Case specific - off by default, so
Hungryandhungryboth match. Turn it on to match the case exactly. - Enabled - untick to keep the trigger without it firing.
- Debugging - shows each run of the action with the captured text filled in, which is the quickest way to see what a trigger matched.
- Action - JavaScript that runs when the trigger fires. The script functions list everything it can call.
The Regex tester checks a pattern against a line you paste in, and while you edit a regex the game text highlights what it would match.
Contains, Regex or Variable updated
Contains fires when the pattern appears anywhere in the text. It is the simplest choice for a fixed message:
- Pattern:
You are hungry - Action:
command('eat bread');
Regex treats the pattern as a regular expression, so it can match text that changes and hand the changing parts to the action. Each pair of brackets is a capture group: textOf(1) is the first, textOf(2) the second, and textOf(0) the whole match.
- Pattern:
^(\w+) waves happily at you\.$ - Action:
command('wave ' + textOf(1));
Variable updated fires when a value changes instead of when text arrives: a GMCP value such as Char.Vitals.hp, or a timer started with startTimer reaching zero. The GMCP triggers guide covers it.
How MUD Client reads the text
Two details explain most surprises.
A trigger can see several lines at once. When the game sends a burst of text, each trigger is handed everything that arrived since it last matched, not one line at a time. In a regex, ^ and $ match at the start and end of every line, but . also matches a line break. So (.*) can run past the end of its line into the lines below. To capture "the rest of the line", use ([^\n]*) instead:
- Pattern:
^(\w+) tells you: ([^\n]*)$
Each piece of text fires a trigger once. After a trigger matches, it carries on from where its match ended, so the same line never fires the same trigger twice. Several matches in one burst each run the action.
Examples
File tells in the Messages panel
- Match type: Regex
- Pattern:
^(\w+) tells you: ([^\n]*)$
addComm(textOf(1) + ' tells you: ' + textOf(2));
playSound('tell');
playSound plays tell.wav or tell.mp3 from the sounds folder in your world's folder; see timers, toasts and sounds.
Catch a message that spans two lines
Put \n in the pattern where the line breaks. This waits for both lines before firing:
- Pattern:
^You have slain ([^\n]+)\.\nYou receive (\d+) experience
log('Killed ' + textOf(1) + ' for ' + textOf(2) + ' xp');
Colour a name
highlight recolours the matched text before it is shown. Colours can be names, hex values or a 256-colour index.
- Match type: Contains
- Pattern:
Grimlock
highlight('red', '');
Hide spam
gag removes the matched line so it never appears.
- Match type: Regex
- Pattern:
^The wind howls[^\n]*$
gag();
substitute('...') replaces the matched text instead of hiding it.
Warn when health is low
With a prompt like HP: 120/400 >, a regex trigger can read the numbers and send a notification, using debounce so a run of prompts warns you once rather than on every line:
- Pattern:
HP: (\d+)/(\d+)
var hp = parseInt(textOf(1), 10);
var max = parseInt(textOf(2), 10);
if (hp < max / 4 && debounce('lowhp', 30)) {
notify('Low health: ' + hp + '/' + max);
}
If your game sends vitals over GMCP, a Variable updated trigger on Char.Vitals.hp does the same without a prompt; the status bar guide shows HP as a gauge.
Keep colours in a captured line
MUD Client matches the words of a line, not its colours, so a pattern cannot ask for "red text". When the colours matter, capture the line and use coloredOf(n) instead of textOf(n): it returns the captured text with its original colours.
When a trigger does not fire
Work down this list:
- Is it enabled? Check the Enabled box, and type
#suspensionsto see whether a script has paused it withsuspendTriggers. - Does the pattern match the real line? Copy the line from the game into the Regex tester. Extra spaces, a full stop, or a colour code you cannot see are the usual culprits. Use
\s+between words if the game sometimes sends two spaces or a tab. - Contains or Regex? In Contains, characters like
.,(and?are plain text. In Regex they are special, so a literal full stop is\.and a literal bracket is\(. - Start of line. With Match from start of line on, or a pattern starting with
^, text in front of the match stops it. Turn the option off, or drop the^, to test. - Case. With Case specific on,
Youandyouare different. - It fires, but with the wrong text. Turn on Debugging to see what each capture held. A
(.*)that swallowed the next line is the most common cause; use([^\n]*). - The action has an error. MUD Client reports a script that fails, along with the error.
log('got here')in the action writes to the console, so you can see how far it ran.
Related
- Regular expression tips - live match highlighting and capture groups.
- Script functions -
textOf,gag,highlight,substitute,debounceand the rest. - Pausing triggers during channel history, with
suspendTriggers. - Getting started - your first trigger and alias.