custom damage formula troubles (if/else, Math.randomInt)

I presently find myself stuck trying to make a skill do a certain thing with the damage formula. Here's what I want it to do:

Target: Self
Lose 1-2 MP, Gain 1-2 TP
If you are at 0 MP, gain 4 TP

I decided to try making this happen via the damage formula and use of gain.Tp/Mp, but I also wanted that conditional second effect to happen at 0 MP: Here's the formula I concocted.

MP Damage, No Element
if (a.mp = 0) { a.gainTp(4); } else { a.gainTp(Math.randomInt(1)+1); Math.randomInt(1)+1; }

Alas, it doesn't seem to be working. What seems to happen when the skill is used is the primary effect of losing MP and gaining TP, but it always seems to be 1 MP lost and 1 TP gained. There's no variation like I wanted from the Math.randomInt component, and the secondary effect of gaining 4 TP at 0 MP just never happens.

I suspect my subpar knowledge on how JavaScript works is the source of my problems. I've already looked through a bunch of guides on here like Damage Formulas 101 to try and figure out how to make it work, but... If anyone more versed in these skills can enlighten me as to how to fix it, I would be most grateful.
Let's see here.
So first of all you didn't use the proper sign to check the subject's current mp.
This "a.mp = 0" should look like one of the following a.mp == 0 or a.mp === 0

Secondly, in your second part of the formula you forgot to add the mp gain.
The correct formula should look something like this:
JavaScript:
if (a.mp === 0) { a.gainTp(4); } else { a.gainTp(Math.randomInt(1)+1); a.gainMp(-Math.randomInt(1)+1); }

I don't think the part of losing 1-2 mp will work correctly if you were to use it in the damage formula. The reason for this is because the formula is evaluated, you lose the mp and when it comes to deal the damage you would deal less.

It might be worth using a plugin to have customized MP Costs.
it always seems to be 1 MP lost and 1 TP gained. There's no variation like I wanted from the Math.randomInt
That is because Math.randomInt() rolls begins at 0, so in order for your variation to happen you need it to be:
a.gainTp(Math.randomInt(2)+1); a.gainMp(-Math.randomInt(2)+1)


randomInt(1) will ALWAYS roll 0
randomInt(2) will be 0-1
randomInt(3) will be 0-2
randomInt(100) would be 0-99

Hope this helps 😀



I don't think the part of losing 1-2 mp will work correctly if you were to use it in the damage formula.
It will work correctly, the only place it would cause issues is if a battler with auto-battle selected/enabled were to use it, then it would do the effects even if the battler picked a different skill.

(part of why I never allow auto-battle, because I prefer to use advanced formula functions like this and avoid the bugs)
Thanks so much y'all! I had been mistaken indeed about Math.randomInt, glad to know how it works for real now. The skill works as intended, 1-2 MP Loss included!

Final Code:
if (a.mp === 0) { a.gainTp(4); } else { a.gainTp(Math.randomInt(2)+1); a.gainMp(-(Math.randomInt(2)+1)); }

I was getting errors for a time, but after some troubleshooting I discovered that the culprit was Yanfly's Damage Core plugin, and I even got it to work by just putting the corrected code in the <damage formula> notebox section. Huzzah!
Note that the other reason to avoid using the damage formula for things like this is for good UI.

What is the damage type of this skill? The damage formula expects a number for the amount of damage it's dealing/healing, and that number will display in a popup.

When the last thing in your formula is a function call (gainTp() or gainMp() ), that has no value so the skill would be expected to show a popup of 0, which makes no sense to the player.

That's why it would be better practice to not have this in the damage formula, but instead in Yanfly Skill Core notetags.

Original thread: https://forums.rpgmakerweb.com/threads/custom-damage-formula-troubles-if-else-math-randomint.174143/