Modifiers โ
Modifiers change how dice results are handled. For example, you can keep the highest dice, re-roll certain results or explode a die when it rolls its maximum value.
All of the built-in modifiers work automatically when you use them in dice notation:
const roll = new Roll('4d6kh3');You only need to customise modifiers if you want to change how they behave. For example, a game might apply modifiers in a different order, or have special rules for which dice can explode.
Modifier execution order โ
Modifiers run in a defined order, regardless of the order you write them in. This matters because one modifier can affect the results another modifier receives.
Each modifier has a handler with an order property. Handlers run from the lowest order to the highest:
| Modifier | Default order |
|---|---|
| Min | 1 |
| Max | 2 |
| Matching-batch explode | 3 |
| Explode | 4 |
| Re-roll | 5 |
| Unique | 6 |
| Map | 7 |
| Keep | 8 |
| Drop | 9 |
| Target success / failure | 10 |
| Critical success | 11 |
| Critical failure | 12 |
| Sort | 13 |
For example, 4d6kh3! and 4d6!kh3 use the same execution order: Explode runs before Keep.
If two handlers have the same order, they run in the order their modifiers appear in the notation.
Changing the execution order โ
You can change the order using a ModifierRegistry. The library provides a registry with all the built-in modifier handlers automatically, so you don't need to create one unless you want to customise something.
First, import the modifiers namespace along with the roll classes:
import { Roll, Roller, modifiers, results } from '@dice-roller/rpg-dice-roller';To change a modifier's order, register a copy of its built-in handler with a different order. For example, to run Keep before Explode:
const customModifiers = new modifiers.ModifierRegistry().register({
...modifiers.keepHandler,
order: 2,
});
const roll = new Roll('8d4kh4!', {
modifiers: customModifiers,
});Keep normally has an order of 8. Changing it to 2 puts it before Explode, which has an order of 4.
The spread (...modifiers.keepHandler) copies the existing handler's behaviour, so you only need to specify what you want to change.
Changing the order can change the results
The default order is intentional. Changing it can affect how other modifiers work together.
In the example above, Keep runs first, but Explode can still roll additional dice for dropped results. Changing execution order alone does not necessarily implement a game's full rules. See Keep before Explode for an example that also changes this behaviour.
Reuse the order across rolls โ
To use the same changes for multiple rolls, pass the registry to a Roller:
// Reuse the customModifiers registry from above.
const roller = new Roller({ modifiers: customModifiers });
roller.roll('8d4kh4!');
roller.roll('4d6kh3!');The roller and all rolls it creates share the registry. You can also pass it to the roll() shorthand.
These changes are not global. Rolls created without your registry still use the default modifier behaviour, so different games can use different rules.
Changing modifier behaviour โ
A modifier handler does more than define execution order: it decides what happens to the dice or group members it receives.
You can replace an existing handler by registering another one with the same name. This lets you change how a modifier works while continuing to use its usual notation.
Adapt an existing handler โ
Suppose you want the Sort modifier to put dropped dice at the end, rather than sorting them alongside the included dice.
You can reuse the built-in sorting behaviour and then move excluded results to the end:
const customModifiers = new modifiers.ModifierRegistry().register({
...modifiers.sortHandler,
apply(items, input) {
const sorted = modifiers.sortHandler.apply(items, input);
// Keep included dice before dropped dice.
return sorted.sort(
(a, b) => Number(b.useInTotal) - Number(a.useInTotal),
);
},
});
const roll = new Roll('4d6kh3s', {
modifiers: customModifiers,
});The original Sort handler still orders the values, but the replacement moves excluded dice after those included in the total.
Keep before Explode โ
Some games require you to choose which dice to keep before checking for explosions, without allowing dropped dice to explode.
To do that, change Keep's execution order and replace the Explode handler so it skips dropped dice:
const customModifiers = new modifiers.ModifierRegistry().register(
{
...modifiers.keepHandler,
order: 2,
},
{
...modifiers.explodeHandler,
*apply(items, input) {
const output = [];
for (const item of items) {
if (item.useInTotal) {
output.push(...(yield* modifiers.explodeHandler.apply([item], input)));
} else {
output.push(item);
}
}
return output;
},
},
);
const roll = new Roll('8d4kh4!', {
modifiers: customModifiers,
});Keep runs first, marking the dice it drops as excluded from the total. The custom Explode handler then applies the built-in explosion logic only to the remaining dice.
This is different from changing the order alone: dropped dice cannot produce extra rolls. Because the built-in Explode handler is a generator, this wrapper also uses *apply and yield* to forward any requests for additional dice.
Writing your own handler โ
For more control, you can create a handler as a plain JavaScript object. It needs a name, order, appliesTo and apply() method.
For example, suppose your game treats any even result as a critical success, rather than only the die's maximum value:
const customModifiers = new modifiers.ModifierRegistry().register({
name: 'critical-success',
order: 10,
appliesTo: ['pool'],
apply(items) {
return items.map((item) =>
item.value % 2 === 0
? results.withFlags(item, 'critical-success')
: item,
);
},
});
const roll = new Roll('4d6cs', {
modifiers: customModifiers,
});The cs notation now marks even dice as critical successes. The withFlags() helper returns a copy of a result with the new flag; it doesn't change the die's value or total.
A handler supports these properties:
| Property | Purpose |
|---|---|
name | The existing modifier that this handler replaces. |
order | When it runs relative to other modifiers. |
appliesTo | Whether it can be used with a dice 'pool', a roll 'group', or both. |
apply(items, input) | Receives results and returns an updated array, or is a generator that returns an array after requesting extra dice. |
groupHandling | Optional setting for how to handle dice inside roll groups. |
The input argument includes the modifier options (input.modifier) and the roll context (input.ctx). For a calculated keep or drop count, the evaluator calculates the count before calling the handler. For example, both 4d6kh(1+1) and 4d6kh2 pass 2 as input.modifier.qty. The original expression stays on the AST, and its evaluated result is stored in modifierArguments on the pool or group result.
For a dice pool, input.die is the pool's die. To request additional dice, implement *apply(items, input) as a generator and use yield* input.rollDie() for one die or yield* input.rollBatch(count) for an entire batch. Both work with synchronous and asynchronous external dice sources and draw from the same per-pool maxGeneratedRolls budget. When the remaining budget cannot cover a request, the helper returns undefined without rolling (or, for batches, without rolling any of the batch). Return the results accumulated so far when the budget stops a repeating modifier.
Handlers that don't need additional dice can continue returning arrays directly. Handlers that do request extra dice must yield through the helpers; simply calling them does not roll a die.
Custom handlers don't add new notation
Register a handler under an existing modifier name to replace its behaviour. The same handler runs whether the modifier comes from notation or from an AST you build directly.
A new modifier name requires support in the library's AST types and validation, even when you build the AST directly. To use the new modifier in a notation string, the parser also needs to recognise its syntax.
Working with results โ
Handlers must ultimately return an array (either directly or from a generator); they should not modify the original items. You can reuse unchanged items and return copies of any items you update.
The results namespace includes helpers for common changes:
| Helper | What it does |
|---|---|
withFlags() | Adds flags to a result, without duplicates. |
withValue() | Changes a die's value and its calculation value. |
withCalculationValue() | Changes how a result contributes to the total without changing the displayed value. |
excludeFromTotal() | Excludes a result from the total, without adding a drop flag. |
These helpers preserve other properties of each result, including information used by presenters and other modifiers.
Modifiers on roll groups โ
If a handler supports roll groups, the optional groupHandling property changes which items it receives:
| Value | Behaviour |
|---|---|
| Not set | Handles the group's members. |
'flatten' | For a group with one expression, handles the dice from its pools together. With multiple expressions, handles the members. |
'each-pool' | Handles group members and also processes the dice inside each pool separately. |
For example, the built-in Keep handler uses 'flatten' and the built-in Sort handler uses 'each-pool'.
For group-level calls, input.die, input.rollDie and input.rollBatch are unavailable. This applies even when the group handler receives individual dice. If you customise group handling, take care to preserve the association between each die and its original pool.
More information โ
See the ModifierRegistry API and ModifierHandler API for more detail, or the results API for the available result helpers and types.