- Introduction to Custom Roll Parsing
- The Structure of a Custom Roll Parsing Function
- The CRP Roll String And Many Buttons
- startRoll and Analysing Your Dice
- Changing Attributes with Custom Roll Parsing
- Using Queries in Custom Roll Parsing
- Computed Properties and CRP Roll Templates
- Callbacks and Promises with startRoll
- Action Buttons and the MacroBar
You can perform any sheet worker code in a Custom Roll Parsing function. You can change attribute values and roll dice simultaneously. A very common desire is to use spell slots or mana when a spell is used, and expend ammunition when a gun or bow is fired.
For example, lets say you have a bow that has an attack bonus based on your level and dexterity, and marks off one arrow every time it’s fired.
on('clicked:shoot', () => {
getAttrs(['level','dex'], values => {
const level = +values.level || 0;
const dex = +values.dex || 0;
const dex_bonus = Math.floor(dex/2 -5);
const level_bonus = Math.floor(level/2);
const attack = dex_bonus + level_bonus;
const roll_string = `&{template:default} {{attack=${attack} }} {{roll=[[1d20+${attack}]] }}`;
const ammo = +values.shoot_ammo || 0;
setAttrs({
shoot_ammo: ammo -1
},{silent:true},startRoll(roll_string, roll => {
finishRoll(roll.rollId);
});
});
});
Code language: JavaScript (javascript)
This sheet worker first calculates an attack bonus, then expends ammo, and makes the roll.
It uses the ability of setAttrs to run a second function after it has completed, but in reality it doesnt really matter what order the setAttrs and startRoll run in, so you could easily do it like this:
on('clicked:shoot', () => {
getAttrs(['level','dex'], values => {
const ammo = +values.shoot_ammo || 0;
setAttrs({
shoot_ammo: ammo -1
});
const level = +values.level || 0;
const dex = +values.dex || 0;
const dex_bonus = Math.floor(dex/2 -5);
const level_bonus = Math.floor(level/2);
const attack = dex_bonus + level_bonus;
const roll_string = `&{template:default} {{attack=${attack} }} {{roll=[[1d20+${attack}]] }}`;
startRoll(roll_string, roll => {
finishRoll(roll.rollId);
});
});
});
Code language: JavaScript (javascript)
Write the code in whichecer way you prefer. Now imagine a special power that uses an amount of power based on the effect you rolled, or marks a power slot only if you succeed! You can do those, too – I’ll leave you to figure out how.