Do variable arithmetic for reduction of Party HP

So I'm making a game without battle sequences, where you touch and then defeat monsters with knockback. Both Parties take damage scaling off Attack and Defense. Thing is, I thus have to treat the Monster HP as a Variable. With script prompts, you can easily substract the difference of the player Attack and the Monsters Defense from the Monsters HP-Variable. To do the same thing in reverse though, I can't reduce the HP of the Player as a Variable, because it isn't one. I have to us the simple "Change HP" command. But that command won't let me use a script prompt. As a consequence, i thought just adding the Defense to the HP first and then subtracting the ATK would solve the problem, but that added Defense can't surpass the Maximum HP, leaving me with the Bug where the first Attack when your player is at Max HP will ignore your defense stat, which is bad for a few interactions. I need some way to change the players HP with a script call, can anyone help?
Here is an example Monster-Event btw:
Not sure for MZ, but in MV you can do:
Code:
$gameActors.actor(1).setHp($gameActors.actor(1).hp - $gameVariables.value(10)

This will subtract whatever value you have in variable 10 from actor 1's HP.
Edit: this is assuming you want a script call and are using the database info - event commands can get you there as well. Also, my apologies- I didn't realize this was for two-way damage so I'll edit the code to reflect that.

Since you are using an event, you can just reference the data directly and calculate from there (this is one way to do it). $dataEnemies references the enemies in your database - a will use the first enemy's attack parameters (param[2]). You could try this script instead of the conditional statements you currently have.

I'll set it up somewhat similarly to the damage formula - the difference here is that you do not have any game troops until you enter into battle set up by the BattleManager so you have to reference the json for your parameters.

Use this script call in your event (edited to reflect two way damage so use the variable number for your enemy's HP - I'll be using v[1] for variable 1):
JavaScript:
const v = $gameVariables._data;
const enemy = $dataEnemies[1];
const actor = $gameParty.members()[0];
const value = enemy.params[2] - actor.def;
v[1] -= actor.atk - enemy.params[3];
$gameParty.aliveMembers().forEach(mem => this.changeHp(mem, -value, true));
From there, run the animation and movement/damage stuff. I used changeHp since it looks like you want the player to be able to lose when HP hits 0. Also, edited to damage entire party, but currently, it is based on the leader's defense only.
What @TESTOSTERONE posted is generally correct (although he's missing a closing parentheses).

It is better to use gainHp() than going directly to setHp(), because just setting it doesn't check everything the game expects to check when HP is lost.

So the better version would be
Code:
$gameActors.actor(1).gainHp(-$gameVariables.value(10))

If you want to do it to the entire party,
Code:
$gameParty.aliveMembers().forEach(actor => actor.gainHp(-$gameVariables.value(10)))
I don't believe scripting is required here; can't this be done with event commands? In RPG Maker, a variable is just a box containing a value. Control Variables starts with that value, does something to it, and replaces the stored value with the result.

E.g. assuming you have a specific actor for "the player":
  1. Store monster attack value in a variable;
  2. Subtract actor defense value from that variable;
  3. Apply a minimum value to that variable (e.g. 0, to prevent healing if DEF > ATK);
  4. Apply damage.
Which can be done with event commands:
  1. Control Variables -> Set -> Variable -> MonsterAttack
  2. Control Variables -> Sub -> Game Data -> Actor -> Defense
  3. Conditional Branch -> Variable < 0:
    • Control Variables -> Set -> Constant -> 0
  4. Conditional Branch -> Variable > 0
    • Change HP -> Variable
      (And any other damage effects.)
I don't believe scripting is required here; can't this be done with event commands? In RPG Maker, a variable is just a box containing a value. Control Variables starts with that value, does something to it, and replaces the stored value with the result.

E.g. assuming you have a specific actor for "the player":
  1. Store monster attack value in a variable;
  2. Subtract actor defense value from that variable;
  3. Apply a minimum value to that variable (e.g. 0, to prevent healing if DEF > ATK);
  4. Apply damage.
Which can be done with event commands:
  1. Control Variables -> Set -> Variable -> MonsterAttack
  2. Control Variables -> Sub -> Game Data -> Actor -> Defense
  3. Conditional Branch -> Variable < 0:
    • Control Variables -> Set -> Constant -> 0
  4. Conditional Branch -> Variable > 0
    • Change HP -> Variable
      (And any other damage effects.)
That would requie me to make yet another self variable for every single monster race which is a hassle and takes ages

What @TESTOSTERONE posted is generally correct (although he's missing a closing parentheses).

It is better to use gainHp() than going directly to setHp(), because just setting it doesn't check everything the game expects to check when HP is lost.

So the better version would be
Code:
$gameActors.actor(1).gainHp(-$gameVariables.value(10))

If you want to do it to the entire party,
Code:
$gameParty.aliveMembers().forEach(actor => actor.gainHp(-$gameVariables.value(10)))
And i can do arithmetic inside the brackets yes?
So a script command
$gameActors.actor(1).gainHp($gameVariables.value(7)-$gameVariables.value(204))
works?
Yes, but as caethyril pointed out above, you really don't need to be using any script calls for this. You can do all of this with regular event commands.
That would requie me to make yet another self variable for every single monster race
To clarify: what I described requires only 1 variable, to store the damage. You can reuse that 1 variable for every damage calculation.

I'd personally avoid scripting here: less room for error, works with Tools -> Event Searcher, and avoids use of eval. Feel free to use the script calls people have suggested, though! 👋

Original thread: https://forums.rpgmakerweb.com/threads/do-variable-arithmetic-for-reduction-of-party-hp.168174/