Help with rolling Damage between two specific values

Hey there, I was wondering if any one had some insight into an issue I'm having. I'm a total amateur trying to code this so I'll explain best that I can.

In combat for attack resolution I first have a hit roll:
Code:
<hitformula>

this._hr = Math.random()  + (a.hit - b.eva);

this._hr > 0

</hitformula>
This part works totally fine but is included because it's value I reference later.
If an attack hits then I have the damage roll. For a basic attack I want to roll damage between a minimum value and a maximum value. What I'd like is for the range of the damage roll to be dependent on how successful the attack roll was. The maximum value will always be the users attack stat, but I want the minimum possible value to be based off of the attack roll, so a very accurate character will be rolling maximum damage often.
So I'd use the following as a function from my own tiny plugin and put $dmgroll(a, b) in the damage formula box in the database:

JavaScript:
/**
 * Random Damage Roll
 */
function $dmgroll(a, b) {
    // minimum and maximum values.
    var min = Math.min(this._hr,1.0) * a.atk; //this._hr is the value taken from the to-hit roll, 0-100%
    var max = a.atk;


    if (b.result().critical) {
        var damage = a.atk; // When Critical calculate damage as maximum
    } else {
        var damage = (int)(Math.random() * (max - min + 1) + min); //roll damage from hit roll success * maximum to maximum //This is the issue as I do not actually know how to code
    }
    return damage;
}

But I have no idea how to roll between these two values, I could cap the minimum with a math method which is what I initially did but that just takes anything rolled under the min and sets it to the minimum rather than roll between the two defined values no? Maybe I'm totally overlooking something but any one have any ideas on how to do this?

Relevant plugins include:
Custom Hit Formula MZ by ATT_Turan
https://forums.rpgmakerweb.com/index.php?threads/custom-hit-formula-mz.156236/
VisuStella Battlecore
Thank you.
Hi there! Please take the time to read through and follow the template. It has READ in all caps with three exclamations marks for a reason.


For example, you are showing a note tag in your post, so you'll need to tell us what plugins you are using, as well as provide a screenshot of where and how you've implemented this.

Just answering the part about "rolling between two values", you could use Math.randomInt(x) for that, a variation of Math.random() that was added in RPG Maker. The damage would be calculated as:

Math.randomInt(MaxDamage - MinDamage) + MinDamage
Hi there! Please take the time to read through and follow the template. It has READ in all caps with three exclamations marks for a reason.


For example, you are showing a note tag in your post, so you'll need to tell us what plugins you are using, as well as provide a screenshot of where and how you've implemented this.

Just answering the part about "rolling between two values", you could use Math.randomInt(x) for that, a variation of Math.random() that was added in RPG Maker. The damage would be calculated as:

Math.randomInt(MaxDamage - MinDamage) + MinDamage
Alright, thanks. I've updated my original post. I've tried what you've said and it's similar to what I've read elsewhere but it seems to result in 0 damage every time, so i think there is a gap in my understanding to implement it

Here is my code which I've imported as a plugin:
JavaScript:
/**
 * Random Damage Roll
 */
function $dmgroll(a, b) {
    // minimum and maximum values.
    var min = Math.min(this._hr,1.0) * a.atk;
    var max = a.atk;

    if (b.result().critical) {
        var damage = max; // When Critical calculate damage as maximum
    } else {
        var damage = Math.randomInt(max - min) + min
    }
    return damage;
}

And here is what the skill in my database looks like:
Having a damage of 0 typically means that there is an error in the calculation somewhere (errors in the damage formula are changed to 0 damage). The problem might be that you are using the "this" keyword within the function, but I'm not sure if it's the correct context, as "this" in the damage formula is Game_Action (you can confirm this through console.log(this) in the damage formula).

Secondly, for your hitformula, a b user target are all valid references for the plugin, but "this" will not refer to the user of the skill. You might be able to make it work by changing all instances of "this._hr" to "a._hr", but I'm not too sure actually. Can do some testing later.

JavaScript:
<hitformula>
a._hr = Math.random()  + (a.hit - b.eva);
a._hr > 0
</hitformula>

EDIT: confirmed that using "this" will neither refer to the user of the skill nor the Game_Action, so that is the cause of your error (and as a result your damage showing up as 0).

I did some testing with the standard "Attack" skill, using a._hr instead of this._hr, and it looks as if it's doing something. Whether it's as you had envisioned, I cannot tell though.
Added this line for debugging:
1759819686259.png

And the result of two turns fighting against two goblins:
1759819563385.png
Having a damage of 0 typically means that there is an error in the calculation somewhere (errors in the damage formula are changed to 0 damage). The problem might be that you are using the "this" keyword within the function, but I'm not sure if it's the correct context, as "this" in the damage formula is Game_Action (you can confirm this through console.log(this) in the damage formula).

Secondly, for your hitformula, a b user target are all valid references for the plugin, but "this" will not refer to the user of the skill. You might be able to make it work by changing all instances of "this._hr" to "a._hr", but I'm not too sure actually. Can do some testing later.

JavaScript:
<hitformula>
a._hr = Math.random()  + (a.hit - b.eva);
a._hr > 0
</hitformula>

EDIT: confirmed that using "this" will neither refer to the user of the skill nor the Game_Action, so that is the cause of your error (and as a result your damage showing up as 0).

I did some testing with the standard "Attack" skill, using a._hr instead of this._hr, and it looks as if it's doing something. Whether it's as you had envisioned, I cannot tell though.
Added this line for debugging:
View attachment 350825
And the result of two turns fighting against two goblins:
View attachment 350824
That's amazing! it does indeed seem to work exactly as I'd hoped for. Thank you so much! And I never knew I could print that type of information to the log, that will be very helpful in the future as well. I appreciate it.
Hooray, happy to have been of help! And yes, console.log() is very important to debugging.

If you haven't already, please report the thread as solved using the ⚠Report button!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/help-with-rolling-damage-between-two-specific-values.180450/