# JavaScript questions that don't deserve their own thread

#### Trihan

##### Speedy Scripter
I'm using YEP_HitAccuracy to have a nicer formula to calculate the sucess of an attack but i'm kinda
confused by what exactly those variables mean

Code:
``skillHitRate * (userHitRate - targetEvadeRate)``

1) SkillHitRate = 'Sucess' parameter on the skill? Success =100% means skillHitRate = 1?
View attachment 182210
2) userHitRate refers to the trait of actors class+extra hit rates from equip, states?
3) A character with no HitRate traits whatsoever would always miss?
4) targetEvadeRate refers to the trait of actors class+extra hit rates from equip, states
5) A character with no Evade Ratetraits whatsoever would always be hit if the attacker were not hadicaped iin hit rate?

I feel like these are basic questions that doesnt deserve they own thread
thanks!
skillHitRate is the listed success rate of the skill (represented as a decimal, so it takes the percentage and multiplies it by 0.01). So yeah, 100% means skillHitRate = 1.

userHitRate and targetEvadeRate are both determined by formulae in the plugin parameters. By default userHitRate is user.hit for physical and 1.0 for magical and certain hit, with targetEvadeRate defaulting to target.eva for physical, target.mev for magical and 0.00 for certain hit. user.hit and target.eva will be the combined value from the battlers's traits and whatever equipment/states etc they have. You are correct: if the battler had no hit rate traits whatsoever the value would be 0 and you'd always miss. A battler with no evade chance would not subtract anything from the user's hit rate.

Let's say your actor has a 80% hit rate from traits and they use a physical skill with 100% success. The enemy has no evade traits. The formula ends up being 1.0 * (0.8 - 0) = 0.8.

Now let's say the actor has 93% hit, the skill has a success rate of 87% and the enemy has 4% evasion. It becomes 0.87 * (0.93 - 0.04) which comes out to 0.7743 rounded to 0.77, so you'll end up with a 77% chance of hitting.

Obviously this is just the default formula used by the plugin; you can change that completely if you wish.

#### matgraz

##### Veteran
Thanks for the reply, i think i get it now!
Now let's say the actor has 93% hit, the skill has a success rate of 87% and the enemy has 4% evasion. It becomes 0.87 * (0.93 - 0.04) which comes out to 0.7743 rounded to 0.77, so you'll end up with a 77% chance of hitting.
How does it round? Does it round down, round up and down (0,5 rounds up), or just getting rid of the last too decimals?

I also have a new question: how does the 'Variance' rate of a skill is calculated? Does it vary for more and less damage?

#### Trihan

##### Speedy Scripter
Thanks for the reply, i think i get it now!

How does it round? Does it round down, round up and down (0,5 rounds up), or just getting rid of the last too decimals?

I also have a new question: how does the 'Variance' rate of a skill is calculated? Does it vary for more and less damage?
View attachment 182215
Accuracy doesn't actually round, I did that in the post to simplify what the hit chance would be. In my example you would actually have a 77.43% chance since the value calculated is compared against a Math.random() so there's no loss of rate. The only thing that rounds is the final damage value, which will round down if the decimal part is less than 0.5 and up otherwise.

Variance will cause the final calculated damage value to vary +/- that percentage. So where you've got (a.atk - b.def / 2) / 2, say a's atk is 29 and b's def is 10, you'd get (29 - 10 / 2) / 2 = (29 - 5) / 2 = 24 / 2 = 12 damage. With a variance of 25%, that will be anywhere from 15 to 9.

#### matgraz

##### Veteran
Variance will cause the final calculated damage value to vary +/- that percentage. So where you've got (a.atk - b.def / 2) / 2, say a's atk is 29 and b's def is 10, you'd get (29 - 10 / 2) / 2 = (29 - 5) / 2 = 24 / 2 = 12 damage. With a variance of 25%, that will be anywhere from 15 to 9.

I see! That's a lot of variation for a bigger number, but no variation at lower values, i'm aiming at working with smaller numbers.

So let's say i'd like to built in the formula some minor variance, and here i'm referencing Super Mario RPG's attack formula, i'd like to know how to write in RPG Maker damge calculation

Code:
``Math.trunc( max (1, ( (a.atk +rand(+var, -var) - b.def)*Timing)``

max = choses the bigger number between 1 and the formula
rand = choses a random number between the variables
+var, -var = those variables are set by the actor's (WeaponAtk x 0.1)
*Timing = the timed hit sucess , can be x2 or x4

How can i write something like that in the damage formula? How do i make the game
1) Choose a bigger number (max)
2) Pick a random value from set choices of numbers e/or variables (rand) ?
3) How do i reference stuff out of the base parameter like the user current weapon Atk value, or someething like current armor Def value (-var, +var)?

Thanks!

#### Trihan

##### Speedy Scripter
You can enter a complex formula directly into the damage formula box in the database; it's evaluated just like your plugin formula is.

For a max, you can do Math.max(min value, max value) and it will take the larger of the two. Math.randomInt(number) will give you a random integer up to the number you supply.

Referencing stuff like weapon attack value would be something along the lines of a.equips()[0].atk but you'd need a couple of sanity checks to make sure that a is an actor and that they actually have a weapon equipped, otherwise it'll crash.

#### CHKNRAVE

##### Surprisingly not sponsored by Yanfly.
Hi, I'm in the process of writing a plugin to skip the file screen.

What it needs to do is save on slot 1 whenever I use the event command to save, and load slot 1 whenever I select the option to load on the title screen.

I found the pieces of code I need to save and load without the file screen, but now I can't find which functions I need to replace. I've been combing through the 8 files in the js folder for a few hours, trying a few things, but so far I only managed to have one save file in the list. Can somebody help me out?

JavaScript:
``````//Save
\$gameSystem.onBeforeSave();
if (DataManager.saveGame(index)) {
StorageManager.cleanBackup(index);
}
\$gamePlayer.reserveTransfer(\$gameMap.mapId(), \$gamePlayer.x, \$gamePlayer.y);
SceneManager.goto(Scene_Map);
}``````

#### matgraz

##### Veteran
Math.randomInt(number) will give you a random integer up to the number you supply.
Sorry, i didnt quite understand this, lets say i have

Code:
``Math.randomInt(-2.5, 2.5)``

Does that mean it'ill give me one of the those two numbers provided, but coming out as a rounded integer, being them -3 OR 3 ? Or does it return numbers BETWEEN -3 and 3 ?

Thanks again !

#### CHKNRAVE

##### Surprisingly not sponsored by Yanfly.
Sorry, i didnt quite understand this, lets say i have

Code:
``Math.randomInt(-2.5, 2.5)``

Does that mean it'ill give me one of the those two numbers provided, but coming out as a rounded integer, being them -3 OR 3 ? Or does it return numbers BETWEEN -3 and 3 ?

Thanks again !
As its name implies, `randomInt` returns a random integer, so the function probably won't like your decimal number. Let's work around this.

Let's say you want a random number between -2.5 and 2.5. The function returns a random integer between 0 and the argument. To calculate the space between your minimum and your maximum, you can use `max - min`, so `2.5 - (-2.5)` here which equals to 5.

Put that as the argument of the function, and you've got a random number between 0 and 5. Subtract 2.5 from the output, and you'll have a random number between -2.5 and 2.5.

The go-to formula for `Math.randomInt` to return a number between `min` and `max` is `Math.randomInt(max - min) + min`

If the argument you'll have to put into the function is a decimal, you're better off multiplying it by 10 times the number of decimal places tolerated, and divide the output by that same multiple of 10. Something like `Math.randomInt((max - min)*100)/100 + min`

#### matgraz

##### Veteran
Sorry, i'm not code savvy at all, i have a few questions, sorry if they are kinda dumb lol

The function returns a random integer between 0 and the argument.
Wouldnt it return a Integer number between -2,5 and 2,5 ? could be -2,-1, 0, 1, 2

Subtract 2.5 from the output, and you'll have a random number between -2.5 and 2.5.
Why we're subtracting 2,5 if went through the trouble of making it a integer number? I didnt get this step

What i intended to to was to create a formula that would take or add some of the damage, i'm not using the 'Variance' parameter in the interface bc it doesnt effect small numbers, and i want my game to use low numbers
For e.g.
In Lvl 1, i want my character with certain weapon, against certain enemy to hit 3, but sometimes 2 and sometimes 4. But i'd like that number to scalate balanced while in higher levels, so with the same enemy, if the base damage was 20, i'd like it to vary between 18 and 22, and so on

#### ATT_Turan

##### Forewarner of the Black Wind
One could also just use the regular Math.random rather than Math.randomInt, no?
Code:
``Math.random()*6-2.5``

Wouldnt it return a Integer number between -2,5 and 2,5 ? could be -2,-1, 0, 1, 2
No, if you actually read Trihan's original post, the function is
Code:
``Math.randomInt(number)``
. He is telling you what the function does. If it takes one number as its argument, that's what it does - you can't just put additional values in and expect something to happen, the code will crash.

Last edited:

#### matgraz

##### Veteran
One could also just use the regular Math.random rather than Math.randomInt, no?
But would it round the numbers so i dont get decimals as damage?

No, if you actually read Trihan's original post, the function is
I read it several times, but i'm not familiar with code at all and english is not my native language, so idk exactly what 'Math.randomInt(number) will give you a random integer up to the number you supply' actually means. Bc to me returning a integer number sounds like that -2,-1, 0, 1, 2 thing, i'm not getting what it actually does

you can't just put additional values in and expect something to happen
You mean it can't be more than 2 values?

Also, what exactly means an 'argument' in the context?

#### ATT_Turan

##### Forewarner of the Black Wind
But would it round the numbers so i dont get decimals as damage?
No. I was addressing the post to Trihan, as he must have had a reason to recommend you use randomInt instead of random.
I read it several times, but i'm not familiar with code at all and english is not my native language
That's okay, but the important part is where he typed "Math.randomInt(number)". "Number" does not mean "multiple numbers separated by commas," it means one single number.
so idk exactly what 'Math.randomInt(number) will give you a random integer up to the number you supply' actually means.
"Will give you a random integer" means this function will return a random value that is a whole number. "Up to" means not higher than. "The number you supply" means whatever single number you typed where he said (number). So if you type Math.randomInt(5) you could get 0, 1, 2, 3, 4, or 5.
You mean it can't be more than 2 values?
I mean it can't be more than 1. It takes one number, which is the maximum value you will get back. The minimum is always 0. That is why Trihan was showing you how to do further math to the result to make it fit into the number range you want.
Also, what exactly means an 'argument' in the context?
An argument is any kind of information you send to a function inside the parentheses ().

#### CHKNRAVE

##### Surprisingly not sponsored by Yanfly.
Sorry, i'm not code savvy at all, i have a few questions, sorry if they are kinda dumb lol

Wouldnt it return a Integer number between -2,5 and 2,5 ? could be -2,-1, 0, 1, 2
Exactly, this is why putting a decimal number as the argument has 0 chance of outputting that number. It's always going to be rounded down. (or up for negative numbers, basically the decimal part is chopped off)
Why we're subtracting 2,5 if went through the trouble of making it a integer number? I didnt get this step
It's because `Math.randomInt(5)` would output an integer between 0 and 5, so subtracting 2.5 would result in a number between -2.5 and 2.5, but then again it wouldn't work very well.
What i intended to to was to create a formula that would take or add some of the damage, i'm not using the 'Variance' parameter in the interface bc it doesnt effect small numbers, and i want my game to use low numbers
For e.g.
In Lvl 1, i want my character with certain weapon, against certain enemy to hit 3, but sometimes 2 and sometimes 4. But i'd like that number to scalate balanced while in higher levels, so with the same enemy, if the base damage was 20, i'd like it to vary between 18 and 22, and so on
You could use the linear `mx+b` formula, one of the first uses of variables you learn in math class, where `x` is the level. Yes, this thing is finally going to have a real-life application.

For example, if the minimum damage is 2 at level 1 and 18 at level 10.
f(x) = mx+b; f(1) = 2; f(10) = 18
On Geogebra, place 2 points with the coordinates being `(level, value)`, then trace a line between them. The website will give you the formula.
For the minimum in this example, the points' coordinates are (1, 2) and (10, 18), so Geogebra says the formula of the line is 1.7777x + 2.2222.

Same thing for the maximum, 4 at level 1 and 22 at level 10.
The formula, this time, is 2x + 2.

Let's use the go-to formula, `Math.randomInt(max-min) + max` with `min = 1.77 * a.level + 2.22` and `max = 2 * a.level + 2`.

Your damage formula that scales with the user's level is: `Math.randomInt((2 * a.level + 2)-(1.77 * a.level + 2.22)) + (2 * a.level + 2)`

Last edited:

#### matgraz

##### Veteran
"Number" does not mean "multiple numbers separated by commas," it means one single number.
That makes sense now! Now i understand what you were talking about, i was confused bc i'm trying to replicate the Super Mario RPG damage formula

Code:
``Int( max (1, ( (a.atk +rand(+v, -v) - b.def)*TimedHit)``
(its not written in java as you can tell, i got from gamefaqs or something.

Here rand(+var, -var) will randomly pick one of those values, a positve or negative. V= weapon.attack * 0.1 .
If Mario is lvl 1 (Attack=20) and has a weapon attack = 20 (v= 20 x 0.1 =2), against a goomba with defense = 3, with no timed hit (TimedHit=1)

The formula would be:
Int( max(1, ((20+20 +rand (2,-2) - 3) *1
Int (max(1, (40 + 2 -3) *1
Int (max(1, 39) *1
Int = 39

In this example the damge output would any integer number between 39 (in case v=2) and 35 (in case v=-2). This is the kind of variation i'm trying to get for my formula, i just think that calculating 'V' by weapon.attack*0.1 requires too much checking to work, could be any number that you give this kind of minor variation scalating proportionally in higher levels

You could use the linear `mx+b` formula, one of the first uses of variables you learn in math class, where `x` is the level. Yes, this thing is finally going to have a real-life application.
What are 'm' and 'b'?

Let's use the go-to formula, `Math.randomInt(max-min) + max`
I still don't get why we'd sum the max at the end, i not getting the logic behind this. Isn't it easier just to calculate (2.max-min) since we're just adding it at the end anyway?

#### ATT_Turan

##### Forewarner of the Black Wind
That makes sense now! Now i understand what you were talking about, i was confused bc i'm trying to replicate the Super Mario RPG damage formula

Code:
``Int( max (1, ( (a.atk +rand(+v, -v) - b.def)*TimedHit)``
(its not written in java as you can tell, i got from gamefaqs or something.
That's fine, but mathematical notation is not the same as computer code in any given language. You're being given instructions on how to do this in Javascript.
What are 'm' and 'b'?
I still don't get why we'd sum the max at the end, i not getting the logic behind this. Isn't it easier just to calculate (2.max-min) since we're just adding it at the end anyway?
You're still making things up. You can not treat a computer function like math or English where you can just write whatever you want. When a function takes one value inside a parentheses, it takes one value, you can't somehow type a number, then a decimal point, and...what you typed doesn't even work mathematically.

This thread is not for tutorials on Javascript or how to do mathematical computations generically in computer code - it's for short questions that don't deserve their own thread. There are other threads on these forums for JS tutorials, as well as many Web sites and videos.

Putting Math.random()*6-2.5 into your code should give you the numbers you want. If you don't understand why, you can Google tutorials on Javascript Math.random - there are several pages with very detailed explanations.

#### CHKNRAVE

##### Surprisingly not sponsored by Yanfly.
I still don't get why we'd sum the max at the end, i not getting the logic behind this. Isn't it easier just to calculate (2.max-min) since we're just adding it at the end anyway?
Because that'd return the wrong numbers.
If your `max` is 5 and your `min` is 0, `Math.randomInt(5-0)+0` (so `Math.randomInt(5)`) would return a random number between 0 and 5.
Using your formula, you get `Math.randomInt(2*5-0)` (so `Math.randomInt(10)`) which would return a random number between 0 and 10.

How the function works, to make it simple and not technical, is that it gets every possible integer between 0 and the number you specify, and then it picks one at random to return.
So for `Math.randomInt(5)` it gets 0, 1, 2, 3, 4, and 5. And then it picks one of them.

If you want a number between 2 and 7, the formula is `Math.randomInt(5)+2`.
As we've seen before, the smallest number the function will return is 0, and the biggest is what you've put between parentheses.
The function by itself, as we've seen just above, results in an integer between 0 and 5, so we add 2 outside of the function so that both the minimum and the maximum are increased by 2, resulting in a number between 2 and 7.

Last edited:

#### matgraz

##### Veteran
I'm using MOG_BattleHud and i'm trying to figure out what is the reference of the image Layout.png and Face.png to draw its X,Y axis from.
For e.g., the HP gauge draws its reference from the Layout.png position, so its 0,0 axis = the Layout.png X,Y, but i can't figure out what is the Layout.png X,Y so i can calculate properly

Does anyone know this?

#### ATT_Turan

##### Forewarner of the Black Wind
You'll probably get more responses for this in Plugin Support rather than in an existing thread for questions about JavaScript.

But I'll take a look at the plugin this evening if you haven't gotten any better responses yet.

#### matgraz

##### Veteran
Ok! I figured this could also work as 'Plugin questions that doesnt deserve its own thread' hahaha
I'll make a topic in the forum, thanks man

#### lokirafael

##### Veteran
Is there a way to check the user base param in MZ?

user.agi gives me the total agi. I just want the base wo equip or buff value.

Thanks

---------- Edit ------------

Alright. 2h trying to figure it out and when I give up and post it I remember I can use user.paramBase(6).

u.u

Last edited:

### Latest Profile Posts

I feel so dumb I forgot to set parameters and thought the plugin wasn't working
I can come up with all of the magic skills in the world, but I can hardly think of creative physical weapon skills within the JRPG realm. One of those ones....
It's time for episode 19 of our Make a Cliff Map in RPG Maker MZ... I mean, Make a Game in RPG Maker MZ! Today we're working on our cliff map again. Let's see if we can finish it this time!

A side story is almost getting into the next stage. Almost everything in this game has real-life references.

Everyday life is full of absurdity. This is just a reflection.