DiceRoller plugin 1.0 (for MV, might work for MZ too)

WiegrafDX

Villager
Hello everyone!

I've dipped here and there at times to check on plugins and sometimes to ask questions but I felt I needed to at least contribute a little as well. Hence, while my scripting / coding has been rusty due to being out of practice, it also gave me an opportunity to dive right back into it as well.

INTRODUCTION

So first off, I've often made searches for a simple plugin to emulate dice rolls and it is possible I've just been unlucky but I never did come across one. So I figured I might as well make my own and after testing it, it seems to be working pretty good for the needs I had of it.

But then, I hear you say "Why not just use a Math.random() function to generate a random value between X and Y? My simple answer is "because of odds".

For let's say that you want to emulate a classic 3d6 rolls for stats in a D&D game, the Math.random() method would allow you to generate a value between 3 and 18 with ease... however, that method would result in each possible value having the same odd, which is 1 in 16. This will possibly satisfy most people as you then have a desired result between 3 and 18.

However, with a proper dice roll, the odds of getting the most extreme values on a roll are FAR lower than say getting a 10 or 11 simply because of the distribution of results from each possible outcome of each die. For example, landing a 3 or 18 with an actual dice roll? You can only achieve it if you roll all 1s or all 6s (which is about 0.4%)... but to get 10? You have a lot more combinations possible that will result in 10 ([6,3,1], [6,2,2], [6,1,3], [5,3,2], etc.) meaning that the results will more often than not be near 10-11 (about 12.5%).

Hence the need for a proper dice roller function to emulate an actual roll of dice.

HOW IT WORKS

So as you might imagine, making a dice roll function isn't that difficult... it just needs to be made really to be usable and be called when needed with a few features to make it easier to use.

Basically, I've created the diceroll() function which, when called, requires 2 parameters : the number of die and the type of die. Thus, if you wanted to perform the aforementionned "3d6", you call a script function and enter :

JavaScript:
``diceroll(3,6);``

The function will then perform 3 consecutive Math.random() functions where a value between 1 and 6 will be generated then added to their previous result, returning the sum of all three. This will effectively and properly generate a dice roll with proper odds associated with that kind of roll.

Of course, since it is a custom function, it means that you can call it in your damage formula for skills and even use battler parameters as the parameters to send to the function to generate those rolls. So you could very well end up creating some sort of "D&D-like" weapon attack roll like so :

JavaScript:
``diceroll(2,4) + Math.Int((a.atk - 10) / 2)``

.. which would be the same (somewhat) to using a 2d4 damage weapon and adding your character's STR (ATK?) mod to the damage itself.

But then, what if you wanted to make a standalone roll and use it in your game later on like, let's say, a skill check of some sort? Well I've added a plugin parameter to let you drop the result of your roll into any one game variable you want.

As long as the plugin parameter "DiceRollVar" value is 1 or greater, then the result of your rolls will ALWAYS be copied into that game system variable so you can reserve and name a variable in your game to receive those precious dice. If the value is 0 or less then it will simply ignore that step entirely.

GIMME!

Here's the code in its entirety :

JavaScript:
``````//=============================================================================
// WIEG_DiceRoller.js
//=============================================================================

/*:
*
* @author Wiegraf
* @plugindesc Creates a function that replicates dice rolling.
*
* @param DiceRollVar
* @desc Variable ID where the result is copied to (none if 0)
* @default 0
*
* @help
* | Information |
* This simple plugin's purpose is to simulate actual dice rolling probabilities
* instead of relying on the usual method of using a random number generator
* range to obtain a similar result.
*
* The difference lies in the probability of each value occuring when generating
* such ranges. For example, taking the example of wanting to simulate a roll of
* 2 six-sided dice (which would result in a value between 2 and 12).
*
* Using the Math.rand() function to generate a value between 2 and 12 would mean
* that each of the 11 possible values would have 1 in 11 chance (or 9.0909%) of
* occuring by using "Math.rand() * 11 + 2".
*
* However, with a roll of 2 six-sided dice (or 2d6), the probability that you
* would roll either a result of 2 or 12 are both 1 in 36 (or 2.7777%) while
* rolling a result of 7 would be 6 in 36 (or 16.6667%) due * to the result being
* generated by 2 random checks of ranges of 1 to 6 instead of a single check of a
* range of 2 to 12.
*
* | Configuration |
* The plugin has a single configurable parameter named "DiceRollVar" which
* determines into which variable * ID the result of the dice roll will be copied.
* By default, the value is set to "0" which ignores that step, meaning the result
* isn't copied to a variable. If set to any value greater than 0 however, the result
* will be copied into the matching variable with the ID of the value entered.
*
* This allows the user to simply use the diceroll() function to quickly generate
* a number that can then be accessed through the ingame variables.
*
* | Usage |
* You can execute the diceroll function though an event with the "Script" advanced
* command.
*
* The "diceroll()" function requires 2 values to be entered in order to function :
* a number of die to be rolled and the type of die. Both values must be positive
* integers (aka rounded numbers). The proper syntax is as follows :
*
* diceroll(number,type) -> Roll "number" die with "type" faces
* diceroll(2,6)         -> Roll 2 dice with 6 faces for a total between 2 and 12
* diceroll(4,8)         -> Roll 4 dice with 8 faces for a total between 4 and 32
*
* Likewise, it is possible to use the diceroll() function in your damage formulas
* as long as you provide * integer-type values as the "number" and "type". For example :
*
* diceroll(a.atk, 4)    -> Roll "a.atk" dice with 4 faces
* diceroll(2,6) + a.atk -> Roll 2 dice with 6 faces for a total between 2 and 12
*                           plus the user's Attack
*
*/

(function() {

//Registers the Plugin for use
var parameters = PluginManager.parameters("WIEG_DiceRoller");
//A place that holds all the parameters from your plugin params above
DiceRollVar: Number(parameters['DiceRollVar'])
};

diceroll = function(dieNumb, dieType) {
var i;
var total = 0;
for (i = 0; i < dieNumb; i++) {
total += Math.floor(Math.random() * dieType + 1);
}
}
};

})();``````

I only ask that you credit me if you use that plugin in your game.

END WORD

If you ever have questions or suggestions of features to add to this plugin, let me know and I'll consider it.

Realist
well its hsort for MV, it doesn't work for MZ unless they add, @target MZ
and than work as well for MV not sure if it's helpfull, but who knows, I save
it anyway, who knows when it comes in handy.

Zaven

Veteran
Thank you for this. If it were now possible to read out notetags from weapons and opponents, such as <dice 2, 3> and insert the variables from the tags into the attack formula, then you would really have weapons with 2d3 and the like. I tried that by setting the attack value and the luck value of the character (I didn't need luck in the game) to 0 and only took both of them from weapons and diceroll works fine with that. But that means the two values for the character are dropped and 2d3 + baseattack is no longer possible. (By the way, I´m using MZ)

Born2Raise_Hell

Warper
Thank you WiegrafDX, I'm just having trouble on where to call if I want the weapons to just roll damage. I know total newbie question! But where exactly do I call the script diceroll(2,6) + a.atk if I want it to work with for Weapons? Do I have to attach a skill to it, then call the script command through common events? Can I call it in the Notes?
Thanks for your time and this was exactly what I was looking for!

Latest Profile Posts

Well, I've got a party tomorrow, and my friends are coming over. I wanted to work a bit on the Fandom Scouts assets so I can be ready when that update arrives... in other news, Pizza Time Horror is getting a second teaser, and it shows Cabaret Helen's last performance before the location closed.
Also who used to be scared of Chuck E as a kid? I was! Now I love him!
The new cards and what they do:

(I am clearly not a youtuber... Haha)
Just for the record you CAN use the "Exclude unused files" successfully to trim a project down seriously even if you're doing calls to various assets via plugins and such. BUT you need to do a full playtest of pretty much every option to ensure you did get every one of those other assets added in. Then I dumped the assets into a fresh copy of the project and yay it was like 400 Meg......

If you've played Knight Shift Chapter 1 this image'll mean something to you. If not, that's also fine!