Debuff in RMMV

Hello everyone, I'm trying to learn how to utilize javascript in RMMV


However I have a problem regarding increasing or decreasing parameter when using a skill.


For example, I would like to create a stacking decrease defense to the enemy.


Usually you just can do something like this, right?


target.def = target.def - someNumber;


However, that code does not work. The result is always NaN when I use console.log. I don't know what's wrong.


Then, I looked at one of Yanfly Tips and Tricks and I realize that he was using addDebuff.


I really want to know how debuff works in RMMV. This is my code:


<Custom Apply Effect>
target._stackingBreak = target._stackingBreak || 0;
target._stackingBreak += 1;
var defRatio = target._stackingBreak * 0.2;
var defReduct = Math.floor(defRatio * target.def);

//This is working (I use console.log to return the value)
var currentDefense = target.def - defReduct;

//These are not working (The target.def won't change its value)
target.def -= defReduct;
target.def = currentDefense;
</Custom Apply Effect>


The plugin I used is Yanfly Buff and State Core


Thanks for the help!
It's because target.def is a getter rather than a setter:


def: { get: function() { return this.param(3); }, configurable: true },


This is how Game_BattlerBase.prototype.param actually works:


Game_BattlerBase.prototype.param = function(paramId) {
var value = this.paramBase(paramId) + this.paramPlus(paramId);
value *= this.paramRate(paramId) * this.paramBuffRate(paramId);
var maxValue = this.paramMax(paramId);
var minValue = this.paramMin(paramId);
return Math.round(value.clamp(minValue, maxValue));
};


Where Game_BattlerBase.prototype.paramRate and Game_BattlerBase.prototype.paramBuffRate are these:


Game_BattlerBase.prototype.paramRate = function(paramId) {
return this.traitsPi(Game_BattlerBase.TRAIT_PARAM, paramId);
};

Game_BattlerBase.prototype.paramBuffRate = function(paramId) {
return this._buffs[paramId] * 0.25 + 1.0;
};


Typically debuffs with paramId is implemented by setting Game_BattlerBase.prototype._buffs[paramId] as negative, which is done via Game_BattlerBase.prototype.decreaseBuff:


Game_BattlerBase.prototype.decreaseBuff = function(paramId) {
if (!this.isMaxDebuffAffected(paramId)) {
this._buffs[paramId]--;
}
};


Where Game_BattlerBase.prototype.isMaxDebuffAffected is this:

Code:
Game_BattlerBase.prototype.isMaxDebuffAffected = function(paramId) {
    return this._buffs[paramId] === -2;
};
It's because target.def is a getter rather than a setter:



def: { get: function() { return this.param(3); }, configurable: true },


This is how Game_BattlerBase.prototype.param actually works:



Game_BattlerBase.prototype.param = function(paramId) {
var value = this.paramBase(paramId) + this.paramPlus(paramId);
value *= this.paramRate(paramId) * this.paramBuffRate(paramId);
var maxValue = this.paramMax(paramId);
var minValue = this.paramMin(paramId);
return Math.round(value.clamp(minValue, maxValue));
};


Where Game_BattlerBase.prototype.paramRate and Game_BattlerBase.prototype.paramBuffRate are these:



Game_BattlerBase.prototype.paramRate = function(paramId) {
return this.traitsPi(Game_BattlerBase.TRAIT_PARAM, paramId);
};

Game_BattlerBase.prototype.paramBuffRate = function(paramId) {
return this._buffs[paramId] * 0.25 + 1.0;
};


Typically debuffs with paramId is implemented by setting Game_BattlerBase.prototype._buffs[paramId] as negative, which is done via Game_BattlerBase.prototype.decreaseBuff:



Game_BattlerBase.prototype.decreaseBuff = function(paramId) {
if (!this.isMaxDebuffAffected(paramId)) {
this._buffs[paramId]--;
}
};


Where Game_BattlerBase.prototype.isMaxDebuffAffected is this:



Game_BattlerBase.prototype.isMaxDebuffAffected = function(paramId) {
return this._buffs[paramId] === -2;
};

Ah I see, I didn't know that it was a getter.


So, how can I set the parameter then? 


Is it using the decreaseBuff function?
Yes, it's the default approach.


The number of debuff levels you can have is defined by :


Game_BattlerBase.prototype.isMaxDebuffAffected


And the power of each debuff is defined by:

Code:
Game_BattlerBase.prototype.paramBuffRate

Original thread: https://forums.rpgmakerweb.com/threads/debuff-in-rmmv.61412/