A plugin can't see the actor variable

I'm trying to port the RGSS3 script I made (a script that changes the color of the HP bar depending on the remaining HP). I have wrote most of the logic, but for some reason, it gives me a ReferenceError "actor is not defined".


The code:


hpTextColorPicker = function() {
if (actor.hp < actor.mhp * CriticalHPLimit) return this.textColor(CriticalHPText);
else if (actor.hp > actor.mhp * CriticalHPLimit && actor.hp < actor.mhp * LowHPLimit) return this.textColor(HPLowText);
else return systemColor();
};

hpbarColorPicker1 = function() {
if (actor.hp < actor.mhp * CriticalHPLimit) return this.textColor(CriticalHPBar1);
else if (actor.hp > actor.mhp * CritialHPLimit && actor.hp < actor.mhp * LowHPLimit) return this.textColor(HPBarLow1);
else if (CompatMode == true) hpGaugeColor1();
else return this.textColor(HPNormalBar1);
};

hpbarColorPicker2 = function() {
if (actor.hp < actor.mhp * CriticalHPLimit) return this.textColor(CriticalHPBar2);
else if (actor.hp > actor.mhp * CritialHPLimit && actor.hp < actor.mhp * LowHPLimit) return this.textColor(HPBarLow2);
else if (CompatMode == true) hpGaugeColor2();
else return this.textColor(HPNormalBar2);
};

Window_Base.prototype.drawActorHp = function(actor, x, y, width) {
width = width || 186;
var color1 = this.hpGaugeColor1();
var color2 = this.hpGaugeColor2();
this.drawGauge(x, y, width, actor.hpRate(), hpbarColorPicker1(), hpbarColorPicker2());
this.changeTextColor(this.hpTextColorPicker());
this.drawText(TextManager.hpA, x, y, 44);
this.drawCurrentAndMax(actor.hp, actor.mhp, x, y, width,
this.hpColor(actor), this.normalColor());
};




This is the error I'm getting:


ReferenceError: actor is not defined
    at hpbarColorPicker1 (<assume game folder>/js/plugins/HPColor.js:76)
    at Window_MenuStatus.Window_Base.drawActorHp (<assume game folder>/js/plugins/HPColor.js:93)
    at Window_MenuStatus.Window_Base.drawActorSimpleStatus (rpg_windows.js:592)
    at Window_MenuStatus.drawItemStatus (rpg_windows.js:1763)
    at Window_MenuStatus.drawItem (rpg_windows.js:1736)
    at Window_MenuStatus.Window_Selectable.drawAllItems (rpg_windows.js:1250)
    at Window_MenuStatus.Window_Selectable.refresh (rpg_windows.js:1277)
    at Window_MenuStatus.initialize (rpg_windows.js:1703)
    at new Window_MenuStatus (rpg_windows.js:1690)
    at Scene_Menu.createStatusWindow (rpg_scenes.js:808)rpg_managers.js:1756 SceneManager.catchException
In the hpbarColorPicker functions you try to access properties of an actor however the actor variable is not defined, therefore you get the ReferenceError. You can just pass the actor as parameter.

Code:
Window_Base.prototype.hpTextColorPicker = function(actor) {
 if (actor.hp < actor.mhp * CriticalHPLimit) return this.textColor(CriticalHPText);
 else if (actor.hp > actor.mhp * CriticalHPLimit && actor.hp < actor.mhp * LowHPLimit) return this.textColor(HPLowText);
 else return systemColor();
};

Window_Base.prototype.hpbarColorPicker1 = function(actor) {
    if (actor.hp < actor.mhp * CriticalHPLimit) return this.textColor(CriticalHPBar1);
    else if (actor.hp > actor.mhp * CritialHPLimit && actor.hp < actor.mhp * LowHPLimit) return this.textColor(HPBarLow1);
    else if (CompatMode == true) hpGaugeColor1();
    else return this.textColor(HPNormalBar1);
};

Window_Base.prototype.hpbarColorPicker2 = function(actor) {
    if (actor.hp < actor.mhp * CriticalHPLimit) return this.textColor(CriticalHPBar2);
    else if (actor.hp > actor.mhp * CritialHPLimit && actor.hp < actor.mhp * LowHPLimit) return this.textColor(HPBarLow2);
    else if (CompatMode == true) hpGaugeColor2();
    else return this.textColor(HPNormalBar2);
};

Window_Base.prototype.drawActorHp = function(actor, x, y, width) {
    width = width || 186;
    var color1 = this.hpGaugeColor1();
    var color2 = this.hpGaugeColor2();
    this.drawGauge(x, y, width, actor.hpRate(), hpbarColorPicker1(actor), hpbarColorPicker2(actor));
    this.changeTextColor(this.hpTextColorPicker(actor));
    this.drawText(TextManager.hpA, x, y, 44);
    this.drawCurrentAndMax(actor.hp, actor.mhp, x, y, width,
                           this.hpColor(actor), this.normalColor());
};
What is the context of those functions?  They don't appear to belong in any class, so they only have access to the objects that you pass in as parameters, or to global objects.  actor is neither.


If your functions are going to be run within the window classes, I'd change them to that scope - it doesn't solve your problem as actor still needs to be passed in, but it does make it clear where the functions are being used:


Window_Base.prototype.hpTextColorPicker = function(actor)...


Window_Base.prototype.hpbarColorPicker1 = function(actor)...


Window_Base.prototype.hpbarColorPicker2 = function(actor)...
Okay, after some reading of the other scripts and following your suggestions, I was able to get the script working. I'd like to thank both of you.


Now to iron out any other bugs left.
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.
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/a-plugin-cant-see-the-actor-variable.73933/