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:
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:
.. 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 :
Spoiler: Code
COPYRIGHT AND STUFF
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.
本贴来自国际rpgmaker官方论坛作者:WiegrafDX处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:
https://forums.rpgmakerweb.com/threads/diceroller-plugin-1-0-for-mv-might-work-for-mz-too.128510/