# Gain Max HP When Skill kills Enemy?

Discussion in 'RPG Maker MV' started by Twitchy2, Jan 14, 2019.

Tags:
1. ### Twitchy2Villager

Messages:
7
1
First Language:
English
Primarily Uses:
RMMV
I want to open by apologizing for probably missing an important rule or putting this in the incorrect place, but nevertheless.

My question revolves around how programming the damage formula works, I have a programmer background but being new to RPGMaker (Literally last night), I am still a bit confused on how things work and cant find a tutorial or push in the right direction for something like this

So lets say for instance, I have this (100 + a.atk - b.def). This is simple and how the skill will normally work. However, how would I add something like this to that statement:

if (b (dies from attack)) {
a.MaxHP += 10;
}

Max HP in this case will be added to both in this battle and outside that (presuming it doesnt already.)

Can anyone give me a hand on this?

2. ### Aloe GuvnerWalrus

Messages:
1,562
978
Location:
USA
First Language:
English
Primarily Uses:
RMMV
Let's break your question down into 2 distinct parts:

1.) How to figure out if the enemy died or not

The damage formula is evaluated prior to the damage being applied. You could calculate whether the skill *will* kill the enemy or not.

Code:
```const damage = 100 + a.atk - b.def;
if (damage >= b.hp) {
// see part 2
}
damage;
```
Notice that you have to have a statement at the end that has an implicit return of the 'damage' variable. If you don't have that, then it has an implicit return of "undefined", which won't work. I would have preferred if the MV devs had designed it to have an explicit return but ¯\_(ツ)_/¯

There's a problem with this though. The damage formula is evaluated first and then other stuff like physical defense rate, variance, critical hits, guarding, etc. is tacked on afterwards. So you can't actually use the damage formula to figure out if the target will die or not (unless you disable all those other stuff, like I do).

2.) How to increase the max HP of the attacker

There's an event command called "Change Parameter", so we can find the function that corresponds to that event command, and see that it calls the addParam function.
Code:
`a.addParam(0, 10)`
Where "a" is the actor in the damage formula, and 0 means Max HP (1 is Max MP, 2 is ATK, etc.).

##)
If, based on my response to #1 above means that you can't predict whether the enemy will die or not based on the damage formula alone (i.e. you use critical hits, guard state, etc. in your game), then you'll need a different approach. You would need to hook into the function that make the damage value via an alias and put logic on top of that.

Code:
```const alias_Game_Action_makeDamageValue = Game_Action.prototype.makeDamageValue;
Game_Action.prototype.makeDamageValue = function(target, critical) {
const value = alias_Game_Action_makeDamageValue.apply(this, arguments);
if (value >= target.hp && [31, 32].includes(this.item().id)) {
// if the target will die and the skill ID is 31 or 32
}
return value;
};```
If you install this snippet as a plugin, it should work.

Twitchy2 likes this.
3. ### Twitchy2Villager

Messages:
7
1
First Language:
English
Primarily Uses:
RMMV
Sweet!

Let me just make sure I understand this for future reference

Target is a global variable for what the ability will be targeting, so that is that, the numbers (such as 31 and 32) is the index of the skill in the database, and Subject is who used it?

If im correct about this, you just saved me a lot of time and also helped me learn how to begin to do my own work here, thank you very much!!

Aloe Guvner likes this.
4. ### Aloe GuvnerWalrus

Messages:
1,562
978
Location:
USA
First Language:
English
Primarily Uses:
RMMV
Although the damage formula doesn't quite work here, it can still do a lot. Here's a good tutorial about the damage formula, and here's a thread with a lot of ideas.

Not quite, "target" isn't a global variable, it's a parameter passed to the function. If the target is an enemy, then it is an instance of the Game_Enemy class. If the target is an actor, then it is an instance of the Game_Actor class.

Right, you would replace with whatever skill IDs that you want this behavior to happen.

Yup, "this.subject()" is whoever used the skill (an instance of Game_Enemy if the enemy used the skill or an instance of Game_Actor if the actor used the skill).

The code I gave made use of an alias for the "makeDamageValue" so that you can still use the original function that the MV devs created but adding your own functionality. If you're a programmer but not a Javascript programmer, you should read about Javascript's "this", as well as "bind", "call", and "apply".

Twitchy2 likes this.