Changing Attributes with Custom Roll Parsing

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.

Series Navigation<< startRoll and Analysing Your DiceUsing Queries in Custom Roll Parsing >>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.