Adjust HP when increasing MaxHP

Why hello there!


Let me explain my situation first:


- I use Yanfly's Base Parameter Control to let my MaxHP and MaxMP be influenced by my other attributes (actors defense stat for example).


- I also use Yanfly's Battle, Status and Equip Core (among others that should not be relevant)


So my problem now is this:


- When I equip a different - let's say - armor that increases my defense (-> and therefor my MaxHP) the character's current HP does not update (which is normal, but not how I want it).


What I want is:


- That my HP increases by the same amount that my MaxHP increases when I equip something.


- - If my actor is at 30/60 HP and equips an item that increases his Defense (and therefore his MaxHP) by 10, I want him to be at 40/70 HP.


- Same for HP and MP o.c.


I am able to adjust some basic JavaScript, I just didn't manage to fix this. (Since I don't directly add or decrease MaxHP, I thought of maybe requesting for a change in MaxHP while equipping and add the same value to HP, I couldn't get the code to work though :/ )


If someone could write me some code so that I can fix this problem, that would be fantastic!


Thanks


Simon
That's a issue that has nothing to do with the plugins you've listed, so there's nothing wrong with them.
Ehm, yea thanks for the quick answer. 


I know it's not a problem with the plugins, I just listed them for simplification, because they do influence how and where certain formulas are executed.


Also, I know that everything works as intended (from the game's & plugin's perspective), it's just not what I want.


Anyhow, I think I managed to get some kind of a work around that seem to do just fine for now. If I'm sure that it works, I'll post it here.



SImon
Read through this and see if this helps: 
Mathematically, it's pretty simple.


Store hp rate within a variable.


Modify maximum hp though equipment, states, stats changes, etc...


Then multiply the new maximum hp by the stored hp rate value.


So if your actor had 72 out of 120 HP. You would first store the hp rate.


72 / 120 = 0.6


Let's say maximum health increases to 190. We then just multiply by the old hp rate.


190 * 0.6 = 114


Simply recovering HP equal to the difference between the old and new maximum HP values wouldn't be very accurate.
190 - 120 = 70
72 + 70 = 142



142 / 190 = 0.747


This would take the actor from 60% hp to about 75%. If hp is decreased in the same way, you may end up killing the actor in some cases.


Unfortunately, I can't write a code for this in MV, but there's the math.
Ok, I think what I did is solid enough to post it as a solution here. If someone could check it, that would be great 😉


So within YEP_EquipCore.js

  • I changed the "onItemOk" function so that it would store the ratio hp/maxHp before equipping the new item.
  • After equipping the item, it sets the hp proportially to my new maxHp.

So here's what it looks like (line 1009)


Yanfly.Equip.Scene_Equip_onItemOk = Scene_Equip.prototype.onItemOk;
Scene_Equip.prototype.onItemOk = function() {
var hpRate = this.actor().hp / Math.max(1, this.actor().mhp);
var mpRate = this.actor().mp / Math.max(1, this.actor().mmp);
var tpRate = this.actor().tp / Math.max(1, this.actor().maxTp());
Yanfly.Equip.Scene_Equip_onItemOk.call(this);
var max = this.actor().isDead() ? 0 : 1;
//var hpAmount = Math.max(max, parseInt(this.actor().mhp * hpRate));
var newMaxHP = this.actor().mhp;
var newMaxMP = this.actor().mmp;
var newMaxTP = this.actor().maxTp();
//var maxHpGained = newMaxHP - currentMaxHP;
var hpAmount = Math.max(max, Math.round(newMaxHP * hpRate));
var mpAmount = Math.max(max, Math.round(newMaxMP * mpRate));
var tpAmount = Math.max(max, Math.round(newMaxTP * tpRate));
this.actor().setHp(hpAmount);
this.actor().setMp(mpAmount);//parseInt(this.actor().mmp * mpRate));
this.actor().setTp(tpAmount);
this._itemWindow.hide();
this._statusWindow.refresh();
};


Is this solid? I mean, it works but will it cause problems for some reason or another?


Thanks


Simon

Original thread: https://forums.rpgmakerweb.com/threads/adjust-hp-when-increasing-maxhp.66522/