MZ How to hide and show the TPB bar?

I've been recently working on an active TPB rework plugin where only enemies actually have to wait for their TPB bar to fill. I've done this by just checking if a battler is an actor and setting their tpbChargeTime to one if true. Additionally, I also added an !isGuard() and !isRestricted() check because both guarding and restriction states are rendered useless if an actor just blazes through to their next turn. Thus, I only wanted an actor's TPB bar to show if they're guarding or restricted because it's useless otherwise, so I first tried drawing the TPB bar only when an actor's guarding.

Spoiler: Unedited Engine Code
JavaScript:
Game_Actor.prototype.performAction = function(action) {
    Game_Battler.prototype.performAction.call(this, action);
    if (action.isAttack()) {
        this.performAttack();
    } else if (action.isGuard()) {
        this.requestMotion("guard");
    } else if (action.isMagicSkill()) {
        this.requestMotion("spell");
    } else if (action.isSkill()) {
        this.requestMotion("skill");
    } else if (action.isItem()) {
        this.requestMotion("item");
    }
};

Window_BattleStatus.prototype.drawItemStatus = function(index) {
    const actor = this.actor(index);
    const rect = this.itemRectWithPadding(index);
    const nameX = this.nameX(rect);
    const nameY = this.nameY(rect);
    const stateIconX = this.stateIconX(rect);
    const stateIconY = this.stateIconY(rect);
    const basicGaugesX = this.basicGaugesX(rect);
    const basicGaugesY = this.basicGaugesY(rect);
    this.placeTimeGauge(actor, nameX, nameY);
    this.placeActorName(actor, nameX, nameY);
    this.placeStateIcon(actor, stateIconX, stateIconY);
    this.placeBasicGauges(actor, basicGaugesX, basicGaugesY);
};

Window_StatusBase.prototype.placeTimeGauge = function(actor, x, y) {
    if (BattleManager.isTpb()) {
        this.placeGauge(actor, "time", x, y);
    }
};

Window_StatusBase.prototype.placeGauge = function(actor, type, x, y) {
    const key = "actor%1-gauge-%2".format(actor.actorId(), type);
    const sprite = this.createInnerSprite(key, Sprite_Gauge);
    sprite.setup(actor, type);
    sprite.move(x, y);
    sprite.show();
};

Window_StatusBase.prototype.createInnerSprite = function(key, spriteClass) {
    const dict = this._additionalSprites;
    if (dict[key]) {
        return dict[key];
    } else {
        const sprite = new spriteClass();
        dict[key] = sprite;
        this.addInnerChild(sprite);
        return sprite;
    }
};

Spoiler: Edited Engine Code
JavaScript:
Game_Actor.prototype.performAction = function(action) {
    Game_Battler.prototype.performAction.call(this, action);
    if (action.isAttack()) {
        this.performAttack();
    } else if (action.isGuard()) {
        let index = $gameParty.members().map(e => e._actorId).indexOf(this._actorId); //Finds the party index of actor
        Window_BattleStatus.prototype.drawTimeGauge(index);
        this.requestMotion("guard");
    } else if (action.isMagicSkill()) {
        this.requestMotion("spell");
    } else if (action.isSkill()) {
        this.requestMotion("skill");
    } else if (action.isItem()) {
        this.requestMotion("item");
    }
};

Window_BattleStatus.prototype.drawItemStatus = function(index) {
    const actor = this.actor(index);
    const rect = this.itemRectWithPadding(index);
    const nameX = this.nameX(rect);
    const nameY = this.nameY(rect);
    const stateIconX = this.stateIconX(rect);
    const stateIconY = this.stateIconY(rect);
    const basicGaugesX = this.basicGaugesX(rect);
    const basicGaugesY = this.basicGaugesY(rect);
    // this.placeTimeGauge(actor, nameX, nameY);
    this.placeActorName(actor, nameX, nameY);
    this.placeStateIcon(actor, stateIconX, stateIconY);
    this.placeBasicGauges(actor, basicGaugesX, basicGaugesY);
};

Window_BattleStatus.prototype.drawTimeGauge = function(index) { //Newly made function
    console.log(this); //Shows Window_StatusBase instead of Window_BattleStatus
    const actor = this.actor(index);
    const rect = this.itemRectWithPadding(index);
    const nameX = this.nameX(rect);
    const nameY = this.nameY(rect);
    this.placeTimeGauge(actor, nameX, nameY);
}

Window_StatusBase.prototype.placeTimeGauge = function(actor, x, y) {
    if (BattleManager.isTpb()) {
        this.placeGauge(actor, "time", x, y);
    }
};


Window_StatusBase.prototype.placeGauge = function(actor, type, x, y) {
    const key = "actor%1-gauge-%2".format(actor.actorId(), type);
    const sprite = this.createInnerSprite(key, Sprite_Gauge);
    sprite.setup(actor, type);
    sprite.move(x, y);
    sprite.show();
};

Window_StatusBase.prototype.createInnerSprite = function(key, spriteClass) {
    const dict = this._additionalSprites;
    if (dict[key]) { //Can't read dict properly because this(context) is supposed to be Window_BattleStatus so it crashes
        return dict[key];
    } else {
        const sprite = new spriteClass();
        dict[key] = sprite;
        this.addInnerChild(sprite);
        return sprite;
    }
};

Spoiler: Error
Screenshot 2026-02-15 184521.png

Essentially the problem is that whenever I try calling Window_BattleStatus.prototype.drawTimeGauge or any other method using Window_BattleStatus, it uses the super class instead of the child class as the context. I've tried using .call(index), .call(Window_BattleStatus.prototype, index) but nothing has worked so far.

I've been at my ropes for a couple of day so far and I tried reading rmmz_window code but it's so different from the other engine code that I'm completely baffled by everything is says. I've also come up with the idea to just draw the TPB bar invisible and update the color when guarding or just have the bar blip in and out like after attacking an enemy, but once again, rmmz_window code blind folds and beats me over with a bat.

If anyone can explain a solution or a workaround to me like I'm 5 years old I would be really grateful.
Here's what I came up with:

Spoiler: See Code
JavaScript:
/**
* Add in our new visibility method to the update logic.
*
* Also, to prevent redundant updates(if the gauge is not visible),
* we add an additional conditional to perform updates only when visible.
*/
const alias_Sprite_Gauge_update = Sprite_Gauge.prototype.update;
Sprite_Gauge.prototype.update = function() {
    this.updateVisibleState();
    if (this.visible) alias_Sprite_Gauge_update.call(this);
};

/**
* Add in a new method to dynamically change the gauge's visibility.
* This method has the following conditions:
* - The battler must be an actor
* - The gauge is strictly used for TPB(or turn timer basically)
*
* If the above conditions have been met, then move on to the next step:
* - If the actor is either guarding or affected by a restriction(ex: Stunned),
*   then we show the gauge.
*   Otherwise the gauge remains hidden until either condition is met.
*/
Sprite_Gauge.prototype.updateVisibleState = function() {
    if (this._battler.isActor() && this._statusType === "time") {
        this.visible = this._battler.isGuard() || this._battler.isRestricted();
    }
};

My solution is that, if you simply want the gauge to be visible/not visible, then go after the sprite itself.
@TobyYasha I thought the same, but the problem with controlling the visibility like that was that the gauges can/will kind of flash before changing values to zero. Like you select guard and the gauge shows full before going to zero. Same stuff happens with states.
You cannot simply call classname.prototype.call(...) and expect the program to understand what you are doing.

Imagine you call this:
Code:
Game_Actor.prototype.performAction.call(...)
What actor are you referring to?

For you, as a human, it's obvious that there's only one instance of this battle window. But how is the program supposed to know that? There could be 10 windows on your HUD, theoretically.

You must tell the program which instance exactly you are referring to. In this case, it should be something like SceneManager._scene._statusWindow.drawTimeGauge(index).

Furthermore, you can replace

Code:
let index = $gameParty.members().map(e => e._actorId).indexOf(this._actorId);

with

Code:
const index = $gameActors.actor(this._actorId).index()
You cannot simply call classname.prototype.call(...) and expect the program to understand what you are doing.

Imagine you call this:
Code:
Game_Actor.prototype.performAction.call(...)
What actor are you referring to?

For you, as a human, it's obvious that there's only one instance of this battle window. But how is the program supposed to know that? There could be 10 windows on your HUD, theoretically.

You must tell the program which instance exactly you are referring to. In this case, it should be something like SceneManager._scene._statusWindow.drawTimeGauge(index).

Furthermore, you can replace

Code:
let index = $gameParty.members().map(e => e._actorId).indexOf(this._actorId);

with

Code:
const index = $gameActors.actor(this._actorId).index()
This was really helpful, I wasn't aware of how scenes were stored and why I couldn't call that method.

Here's the finished code along with a way to delete the TPB gauge I had to dig around a few hours for.
Spoiler: Finished code
JavaScript:
let tpbBarCreated = false;

Game_Actor.prototype.performAction = function(action) {
Game_Battler.prototype.performAction.call(this, action);
if (action.isAttack()) {
this.performAttack();
} else if (action.isGuard()) {

let index = $gameActors.actor(this._actorId).index()
SceneManager._scene._statusWindow.drawTimeGauge(index);
tpbBarCreated = true;

this.requestMotion("guard");
} else if (action.isMagicSkill()) {
this.requestMotion("spell");
} else if (action.isSkill()) {
this.requestMotion("skill");
} else if (action.isItem()) {
this.requestMotion("item");
}
};

Window_BattleStatus.prototype.drawItemStatus = function(index) {
const actor = this.actor(index);
const rect = this.itemRectWithPadding(index);
const nameX = this.nameX(rect);
const nameY = this.nameY(rect);
const stateIconX = this.stateIconX(rect);
const stateIconY = this.stateIconY(rect);
const basicGaugesX = this.basicGaugesX(rect);
const basicGaugesY = this.basicGaugesY(rect);
// this.placeTimeGauge(actor, nameX, nameY);
this.placeActorName(actor, nameX, nameY);
this.placeStateIcon(actor, stateIconX, stateIconY);
this.placeBasicGauges(actor, basicGaugesX, basicGaugesY);
};

Window_BattleStatus.prototype.drawTimeGauge = function(index) { //Newly made function
const actor = this.actor(index);
const rect = this.itemRectWithPadding(index);
const nameX = this.nameX(rect);
const nameY = this.nameY(rect);
this.placeTimeGauge(actor, nameX, nameY);
}

Game_Battler.prototype.onTpbCharged = function() {
    if (this.isActor && tpbBarCreated) {
        let spriteGauges = SceneManager._scene.children[1].children[1]._additionalSprites; //Sprites for gauges
        const key = "actor%1-gauge-%2".format(this.actorId(), "time"); //Since the additionalSprites objects are named after actor
        spriteGauges[key].destroy();                                   //ids and the type of gauge I have to create a key to
        tpbBarCreated = false;                                         //accurately delete them
    }
    if (!this.shouldDelayTpbCharge()) {
        this.finishTpbCharge();
    }
};
It's probably easier for you to adjust the gauge class to show/hide them individually. I suggest something like this:

JavaScript:
(function() {

const _SpriteGauge_update = Sprite_Gauge.prototype.update;
Sprite_Gauge.prototype.update = function() {
    _SpriteGauge_update.call(this);

    this.visible = this.isVisible();
}

Sprite_Gauge.prototype.isVisible = function() {
    if (this._statusType !== 'time') {
        return true;
        // always visible when it's a HP,MP,TP... gauge
    }
    if (!this._battler) {
        return false; // it may happen that _battler is null because the sprite is not yet initialized completely
    }
    if (this._battler.isEnemy()) {
        return true; // always visible when it's an enemy
    }
    return this._battler._shouldShowTpb;
}

Game_Actor.prototype.performAction = function(action) {
    Game_Battler.prototype.performAction.call(this, action);
    if (action.isAttack()) {
        this.performAttack();
    } else if (action.isGuard()) {
        this._shouldShowTpb = true; // <--
        this.requestMotion("guard");
    } else if (action.isMagicSkill()) {
        this.requestMotion("spell");
    } else if (action.isSkill()) {
        this.requestMotion("skill");
    } else if (action.isItem()) {
        this.requestMotion("item");
    }
};

})()

Now, you can easily toggle _shouldShowTpb true/false on actors. I already made an example how you can switch if on. Now, you only have to find out a way to switch it off again.

This way, you no longer have to create/destroy sprites, as this is usually more difficult.
It's probably easier for you to adjust the gauge class to show/hide them individually. I suggest something like this:

JavaScript:
(function() {

const _SpriteGauge_update = Sprite_Gauge.prototype.update;
Sprite_Gauge.prototype.update = function() {
    _SpriteGauge_update.call(this);

    this.visible = this.isVisible();
}

Sprite_Gauge.prototype.isVisible = function() {
    if (this._statusType !== 'time') {
        return true;
        // always visible when it's a HP,MP,TP... gauge
    }
    if (!this._battler) {
        return false; // it may happen that _battler is null because the sprite is not yet initialized completely
    }
    if (this._battler.isEnemy()) {
        return true; // always visible when it's an enemy
    }
    return this._battler._shouldShowTpb;
}

Game_Actor.prototype.performAction = function(action) {
    Game_Battler.prototype.performAction.call(this, action);
    if (action.isAttack()) {
        this.performAttack();
    } else if (action.isGuard()) {
        this._shouldShowTpb = true; // <--
        this.requestMotion("guard");
    } else if (action.isMagicSkill()) {
        this.requestMotion("spell");
    } else if (action.isSkill()) {
        this.requestMotion("skill");
    } else if (action.isItem()) {
        this.requestMotion("item");
    }
};

})()

Now, you can easily toggle _shouldShowTpb true/false on actors. I already made an example how you can switch if on. Now, you only have to find out a way to switch it off again.

This way, you no longer have to create/destroy sprites, as this is usually more difficult.
THIS SOLVES LITERALLY EVERYTHING THANK YOU.

Spoiler: Code
JavaScript:
const _SpriteGauge_update = Sprite_Gauge.prototype.update;
Sprite_Gauge.prototype.update = function() {
    _SpriteGauge_update.call(this);

    this.visible = this.isVisible();
}

Sprite_Gauge.prototype.isVisible = function() {
    if (this._statusType !== 'time') {
        return true;
    }
    if (!this._battler) {
        return false;
    }
    if (this._battler.isEnemy()) {
        return true;
    }
    return this._battler.isRestricted() || this._battler.isGuard();
}

Original thread: https://forums.rpgmakerweb.com/threads/how-to-hide-and-show-the-tpb-bar.182477/