Attack that gets weaker as MP goes down

ShinySlowqueen

Veteran
Veteran
Joined
Dec 22, 2015
Messages
49
Reaction score
3
First Language
English
Primarily Uses
RMVXA
So I've got a character's skill that uses MP and deals more damage the more MP the character has. I thought this would be simple, but the damage formula doesn't seem to work. I hope someone can tell me what I'm doing wrong.

Code:
((a.atk * (a.mp/a.mmp)) * 4) - b.def * 2
So if a character has 50 ATK and 50/50 MP, the formula, as *my* brain is reckoning it, should be like:
((50 * (50/50)) * 4)
or (50 * 1) * 4
So 50 * 4

If the character has 45 MP left, the formula should work like
((50 * (45/50)) * 4)
(50 * 0.9) * 4
So 45 * 4

If the character has 12 MP left, the formula should work like
((50 * (12/50)) * 4)
(50 * 0.24) * 4
So 12 * 4

But what's happening in battle is that this attack isn't doing any damage *at all.*

I'm really worried the answer is going to be something like "Ruby can't handle fractions and decimals."
 

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
5,592
Reaction score
6,522
First Language
Indonesian
Primarily Uses
RMVXA
I'm really worried the answer is going to be something like "Ruby can't handle fractions and decimals."
Partially, yes, but that is because if an integer is divided by integer, it won't be float/decimal. So if you do 1/2 it will be 0, not 0.5.
What you do is to convert either of the integers into a float by adding ".to_f"

For example:
a = 1
b = 2
"a/b.to_f" or "a.to_f/b" will work.

In your case, you should convert either of "a.mp" or "a.mmp" to a float.
 

Aoi Ninami

Veteran
Veteran
Joined
Sep 22, 2015
Messages
413
Reaction score
513
First Language
English
Primarily Uses
RMVXA
Alternatively, just rewrite the formula as: ((a.atk * a.mp * 4)/a.mmp) - b.def*2

This is mathematically equivalent -- a * (b*c) is the same as (a*b) * c -- but the different evaluation order will prevent the rounding problem.

Using your example with 12 MP left: (50 * 12 * 4)/50 would evaluate as 2400/50 = 48.
 

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
Or, you could just use "mp_rate" instead of "mp/mmp", which is a built-in method of Game_Battler and does exactly what Theo explained. There is an hp version as well called "hp_rate" if you ever need it.

The obvious advantage of this is that it automatically returns the float value of that division, while taking less characters in your formula box (VX Ace has a very limited formula box).
Code:
a.atk * a.mp_rate * 4 - b.def * 2
It is even easier to read because there is no need for parenthesis (which allows you to save even more characters).
 

ShinySlowqueen

Veteran
Veteran
Joined
Dec 22, 2015
Messages
49
Reaction score
3
First Language
English
Primarily Uses
RMVXA
Or, you could just use "mp_rate" instead of "mp/mmp", which is a built-in method of Game_Battler and does exactly what Theo explained. There is an hp version as well called "hp_rate" if you ever need it.

The obvious advantage of this is that it automatically returns the float value of that division, while taking less characters in your formula box (VX Ace has a very limited formula box).
Code:
a.atk * a.mp_rate * 4 - b.def * 2
It is even easier to read because there is no need for parenthesis (which allows you to save even more characters).
Wow, so it was that simple. Super thank you ^^
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD

Forum statistics

Threads
105,868
Messages
1,017,070
Members
137,577
Latest member
SadaSoda
Top