Home Forums Scripting and Macros Hit Die Roll using Math.random

This topic contains 2 replies, has 2 voices, and was last updated by  carl 3 days, 22 hours ago.

  • Author
    Posts
  • #2224

    rwbyers
    Participant

    Carl,

    I’ve tested the following script in the emulator successfully but cannot get it to work in game:

    hitdie=8; // this line is to simulate the manually generated entry or default value
    var a = hitdie; // assigns a to match the entry
    var b = Math.floor(Math.random() * a)+1; //calls on a random generator from 1 to a.
    {b;} // return the value

    Basically what I have done is put a hit dice macro on the sheet for DnD 5E for short rest scenarios, etc that allow you to roll a hit die to heal.

    However, the program never gives me the roll option when hovering over the value. Do I need to call a roll command somewhere???

  • #2225

    rwbyers
    Participant

    Well I got the following to work, sort of. It returns the correct number, but rolls all of the dice in the array and pulls out the indexed value.

    var dice = [“1d4″,”1d6″,”1d8″,”1d10″,”1d12″];
    var index=99;
    if (hitdie==4){index=0;}
    else if (hitdie==6){index=1;}
    else if (hitdie==8){index=2;}
    else if (hitdie==10){index=3;}
    else {index=4;}
    var a=dice[index];
    a;

    and yes the player could just “look” at their HD and select it; I’m just trying to automate the sheet and learn so please bare with my plodding

  • #2229

    carl
    Keymaster

    I think I see what you’re doing there. You want a roll macro that rolls a particular die based on some other value. Dice macros are evaluated as javascript in a sandbox (segregated from the main logic), so dice are rolled first and the results are inserted into the macro be evaluation.

    I’ve been experimenting with mechanisms to drive dice from the macro though I don’t have a clean implementation.

    I can’t think of a better solution then the one you’ve written off hand. If you wanted to do a single die roll, you could roll a d100 and then “math” the value down to what you wanted. Something like:

    var roll = 1d100;
    var ratio = 100 / hitdice;
    var heal = Math.ceil(roll / ratio);
    heal;

    hitdice in this example is some value on the character sheet.

You must be logged in to reply to this topic.