MV Yanfly Tips & Tricks "Leech Seed" but for ~MAaAaGiC~*spooky noises*

So, I was attempting to bastardize the code in the Yanfly Tips & Tricks to create a "leech seed" like effect, but with magic.
To specify what "Leech Seed" is, basically it's a state where the opposing enemy's health is drained and the user is restored by the amount drained.
That's it in short, and, I got this basic part working (I think).
Code is here:
Spoiler
JavaScript:
<Custom Regenerate Effect>
// Check if the original caster isn't the target and that the original target is alive.
if (origin !== target && origin.isAlive()) {
  // Determine the damage dealt.
  var mpleech = target.mp / 9;
  // Round up the damage.
  damage = Math.ceil(mpleech);
  // Play the healing animation on the original caster.
  origin.startAnimation(46);
  // Original caster gains HP.
  origin.gainMp(damage);
  // Show the MP gained.
  origin.startDamagePopup();
  // Clears the original caster's HP popup.
  origin.clearResult();
  // Play the poison animation on the target.
  target.startAnimation(59);
  // Damage the target's HP.
  target.gainMp(-damage);
}
</Custom Regenerate Effect>

There are a few problems with this:
Firstly, it doesn't seem to work, but it could also be that my numbers are too small so it is working, just restoring 0.
So, that's problem #1 I am looking to fix; making sure that it always restores the Player's magic.
  1. I would like to base how much it restores off of the player's stats. (high MAT, high restore.)
  2. It should always restore the player's Magic, even if the enemy has 0 magic to "leech".
Secondly, there isn't a pop up for this.
I see the "origin.startDamagePopup" and tried to see if this command also works for magic, and if not, what is the command for magic popups, however I couldn't find any results from my google searching 🙁

So to summarize what I want:
I'm trying to create a state that: restores the player's MP based on their stats and at the same time depletes the enemy's MP based on the player's stats.

To clarify some stuff:
I am using RPG Maker MV
I am using Yanfly's plugins (obviously)
I have the full package. I don't think other plugins are necessary to achieve this, but if they are, I have access to those other plugins.

Your help is appreciated :LZSsmile:
I don't see how it would be causing the effect to fail, but you do have a mistake where you start off declaring your variable as mpleech, but then the rest of the code references damage.

That's a second variable that's not necessary, and it also causes it to have global scope which is generally undesirable. I'd fix the rest of the code to reference mpleech.

Math.ceil() should be rounding up, so you should always be doing at least 1 - if you're not seeing anything happen, something's wrong. Correspondingly, the popup should work if the gainMp() is getting called. Do you see anything in the console when this runs?

If you put a test at the very top, above the first if, can you verify the state is on there and the notetag is getting evaluated at all?
Code:
console.log("The regen is happening");
  1. I would like to base how much it restores off of the player's stats. (high MAT, high restore.)
  2. It should always restore the player's Magic, even if the enemy has 0 magic to "leech".
You can use origin.mat to reference the user's MAT, and Math.max() to return the highest value amongst a list of values, in case the enemy happens to have 0 magic.

So for example you could do something like var mpleech = Math.max(target.mp / 9, origin.mat).

Math.ceil() should be rounding up, so you should always be doing at least 1 - if you're not seeing anything happen, something's wrong. Correspondingly, the popup should work if the gainMp() is getting called. Do you see anything in the console when this runs?
That's only true if the value is greater than 0; Math.ceil(0) would still round up to 0.
That's only true if the value is greater than 0; Math.ceil(0) would still round up to 0.
However, that could only be true if the target's MP is 0. Otherwise, it will be a decimal value. I'm presuming the OP is savvy enough to be testing on a target that has something.

But nesting it in Math.max() like you said will take care of that edge case.
you do have a mistake where you start off declaring your variable as mpleech, but then the rest of the code references damage
That was my smooth brain making things more complicated then necessary. Mind you, this is just copy-pasted from yanfly's website, but originally in the code I copied, it was referenced as 'damage'. Smooth brain me was like, "well since I am changing this to be mp related, I'll change the name of the variable to be mp related". I changed the name, and one reference to that variable, but neglected to change the rest.

So for example you could do something like var mpleech = Math.max(target.mp / 9, origin.mat)
This worked like a charm! Now, doing full MP is a bit OP, right? I'm assuming I can use this formula: "origin.mat / 8" and it will give me 20% of the MAT, right :LZSskeptic:

Thank you to both of you for your quick response on this! I'll play around with the numbers more tomorrow.
Now, doing full MP is a bit OP, right? I'm assuming I can use this formula: "origin.mat / 8" and it will give me 20% of the MAT, right
Just putting it out there, you seem to be switching back and forth between MP and MAT - I'm presuming you will actually keep it straight in your head, but it's a bit confusing to know which one you mean.

And...well, yes but no? You can just do math, but dividing a number by 8 is not 20%. You could do origin.mat/5 or origin.mat*.2
you seem to be switching back and forth between MP and MAT
Sorry, you'll have to excuse my brain last night, it was late.
but dividing a number by 8 is not 20%
:LZSlol: Any time I deal with math, it's a humbling experience.

Anyways, thank you so much for your help.

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/yanfly-tips-tricks-leech-seed-but-for-maaaagic-spooky-noises.159759/