Better Way To "Freeze" a Player's HP?

I am working on a game that uses an ABS plugin, so all of the action takes place on-map.

There are points when I need to "freeze" the player's HP, and prevent it from suffering damage.

I am currently using a script that uses a variable to store the player's HP:

Code:
    var xActor = $gameActors.actor($.xActorID);
    $.xHPFreeze = xActor.hp;

and then a function that "maintains" the player's HP:

Code:
    $.fRHP = function() {
        if ($.xHPFreeze != undefined) {
            var cActorHP = $gameActors.actor($.xActorID);
            if (cActorHP.hp < $.xHPFreeze) {
                var xHP = ($.xHPFreeze - cActorHP.hp);
                cActorHP.gainHp(xHP);
            };
        };  
    };

It works, it just feels clunky.

Is there a better way to go about doing this?

Is it possible instead to attach a switch to the function that monitor's a player's HP, which can be enabled / disabled to allow / deny damage?

I am sorry if this is a basic question, I am still learning my way around RPG Maker.

Thanks!
I would probably overwrite or alias the executeHpDamage method (Game_Action.prototype.executeHpDamage), assuming that damage still flows through that method in your ABS. Have it check for whether the battler's HP should be frozen (you could do this with a database state, or by adding a new property to battlers in code), and if so, either exit the method early or have it skip the "target.gainHp(-value);" line.
I would probably overwrite or alias the executeHpDamage method (Game_Action.prototype.executeHpDamage), assuming that damage still flows through that method in your ABS. Have it check for whether the battler's HP should be frozen (you could do this with a database state, or by adding a new property to battlers in code), and if so, either exit the method early or have it skip the "target.gainHp(-value);" line.

Wavelength- thank you for the advice / help!
I would probably overwrite or alias the executeHpDamage method (Game_Action.prototype.executeHpDamage), assuming that damage still flows through that method in your ABS. Have it check for whether the battler's HP should be frozen (you could do this with a database state, or by adding a new property to battlers in code), and if so, either exit the method early or have it skip the "target.gainHp(-value);" line.

So, based on your advice- I wrote this, and tacked it on to the end of my plug-in:

Code:
    var execHpDamage = Game_Action.prototype.executeHpDamage;

    Game_Action.prototype.executeHpDamage = function(target,value) {
       
        if ($gameSwitches.value($.xSwitch) != true && target != $gameActors.actor($.xActorID)) {
            execHpDamage.call(this, target, value);
        };
   
    };

$.xSwitch is the switch that indicates whether the HP Freeze is on (true) or off (false).
$.xActorID is the ID of the Character who should have their HP Frozen.

It kinda works, except that when HP Freeze is on, nobody's HP receives damage!

Any clue on what I am doing wrong?

Thanks again for your help!
I think there's a logic error in your check. You are asking for the Switch to be NOT True (thus False) and also for the Actor to NOT be the designated Frozen HP Battler, in order for any given battler to take damage. In other words, if either condition is not met, damage will not be taken. However, if you are simply setting one switch in your game to designate that freeze functionality is on or off, then any time this switch is set to True, the first part of the condition will not be satisfied, and the second part won't matter (because the entire condition will be false, therefore what's inside the branch won't run).

Changing the condition to the following might work:
Code:
if (!($gameSwitches.value($.xSwitch) === true && target === $gameActors.actor($.xActorID))) {
            execHpDamage.call(this, target, value);
        };

With all that being said, if there are multiple battlers that can ever go into this HP Freeze status, then I think a better approach would be to use a Database State (the syntax is: xxxxx.isStateAffected(stateId) where xxxxx is a reference to a battler object such as $gameActors.actor(2) ), or to define an extra Property for battlers (done easily by aliasing Game_Battler.prototype.initMembers - check that method to see the format used) to designate whether they're HP Frozen or not. The advantage of such an approach is that it's much more scalable: you'll never need to do anything on the back end if you decide that more battlers can enter this status, and you won't need to set a game-wide Switch based on whether any of those many battlers are in the status or not.
Wavelength- thank you, so much!

I was going blind just looking over my script trying to figure out why it wasn't working.

I haven't looked at .isStateAffected yet, but for Game_Battler.prototype.initMembers- is defining an extra property as simple as just adding my own ._HpFrozen attribute (and then using that instead of the switch)?

For example:

Code:
var GB_initMembers = Game_Battler.prototype.initMembers;

Game_Battler.prototype.initMembers = function() {
   
    GB_initMembers.call(this);
   
    this._HpFrozen = false;
   
};

And then just, $gameActors.actor($.xActorID)._HpFrozen = true; and checking that instead of $.xSwitch in
Game_Action.prototype.executeHpDamage?

Many thanks!

Original thread: https://forums.rpgmakerweb.com/threads/better-way-to-freeze-a-players-hp.108221/