Trying to make a variable increase when a character is weak

So I have a mood based battle system. What I'm trying to do is have each character have a trigger where their fear mood increases when they enter a weak state. I use a unique variable for each of my three characters combined with a common event to make the mood increase.

I've made similar things happen when a character is targeted for an attack to increase their mood. What I'm trying to have happen here is

1) the character gets low HP
2) their 'fear' variable goes up 20
3) the common event triggers in battle and updates their fear state if the mood variable is high enough.

I thought I could accomplish this by having a unique hidden state always on for each character that checks to see if they're low HP and increase the variable from there, but then the problem is, how do I get it to detect only when they character first reaches the low HP state? I don't want it it keep triggering each turn and I don't want it to trigger only once per battle when HP is low. Ideally I want it to trigger each time hp gets below a certain threshold (like a percentage of HP).

*Edit* To clarify that last part, if HP becomes less than let's say 5 percent of max, then it triggers, character heals, then hp drops to 5 percent again later in battle it triggers.

But in scenario 2 where they are still low HP from past turn it doesn't trigger.

Any idea how I would implement such a precise trigger?
You'd use Yanfly's Passive States and Buffs & States Core.

Give each actor the same passive state with these notetags:
Code:
<Custom Battle Effect>
user.moodHit = false;
</Custom Battle Effect>

<Custom Respond Effect>
if (target.isDying() && !target.moodHit)
{
    target.moodHit = true;
    $gameVariables.setValue(69, $gameVariables.value(69) + 1);
}
</Custom Respond Effect>

That will increment variable 69 by 1 the first time every battle each actor is brought to critical health.
Okay, so what if I'm wanting it to be each time the character is brought to a weak state even in the same battle?

So if they go weak it triggers, then heals, then goes weak again it triggers again. Basically I'm just wanting it to trigger each time it hits the weak state but not trigger every turn if they're already weak.
As I understand it this isn't quite what the OP wanted. I'd try this tweak of @ATT_Turan 's suggestion:

Code:
<Custom Battle Effect>
user.moodHit = false;
</Custom Battle Effect>

<Custom Respond Effect>
if (target.isDying() && !target.moodHit)
{
    target.moodHit = true;
    $gameVariables.setValue(69,
    $gameVariables.value(69) + 1);
}
else if (!target.isDying() && target.moodHit)
{
    target.moodHit = false;
}
</Custom Respond Effect>

This should reset the flag whenever the actor gets healed above critical health by a skill or item, so the code can trigger again.
If you use regen effects in your game though they will need extra handling.
Okay...I get what you're saying, but I don't know what you want to happen in each of those circumstances.

What @eomereolsson posted is what you asked for in your last post. If you also want the variable to decrement when they leave critical, you'd add that in.
Okay...I get what you're saying, but I don't know what you want to happen in each of those circumstances.

What @eomereolsson posted is what you asked for in your last post. If you also want the variable to decrement when they leave critical, you'd add that in.
Nope, that's what I was looking for. Thank you for the help!
As I understand it this isn't quite what the OP wanted. I'd try this tweak of @ATT_Turan 's suggestion:

Code:
<Custom Battle Effect>
user.moodHit = false;
</Custom Battle Effect>

<Custom Respond Effect>
if (target.isDying() && !target.moodHit)
{
    target.moodHit = true;
    $gameVariables.setValue(69,
    $gameVariables.value(69) + 1);
}
else if (!target.isDying() && target.moodHit)
{
    target.moodHit = false;
}
</Custom Respond Effect>

This should reset the flag whenever the actor gets healed above critical health by a skill or item, so the code can trigger again.
If you use regen effects in your game though they will need extra handling.
Right now I don't use regen effects, though I probably will and I do have poison.

I suppose getting it to trigger over and over with Regen and poison would be more of a feature with clever gameplay than a big, unless you're implying unintended stuff could happen with this and Regen.

Either way thanks everyone for the help! I'll test it out ASAP.
unless you're implying unintended stuff could happen with this and Regen.
He's saying that, per the plugin documentation, Respond Effect happens after the battler is targeted with an action.

If they were to heal above critical from regeneration, rather than an action that produced healing, the respond effect would not be checked and the flag would not be turned off.
He's saying that, per the plugin documentation, Respond Effect happens after the battler is targeted with an action.

If they were to heal above critical from regeneration, rather than an action that produced healing, the respond effect would not be checked and the flag would not be turned off.
Hmm. That could be a small problem. I imagine the inverse is true for poison then. If a poison effect makes them weak, the fear variable wouldn't kick in. Seems like a minor but annoying issue.

Does this also mean it wouldn't work if the battler used an ability that required paying health as part of the ability? I have an ability where it does a powerful attack but the user goes down to 1 health as a cost to use it. That probably wouldn't trigger this then, I imagine.
Correct. You could copy all of the code into a custom regenerate or whatever it's called, have an additional time it checks.
Correct. You could copy all of the code into a custom regenerate or whatever it's called, have an additional time it checks.
The custom regenerate effect would solve the regen problem but not the paying health problem, right?

Actually I can just solve the self damage problem by declaring it a feature and not a bug, lol.
No, it would run the check every regeneration phase. Then if the condition is met, the code would happen, regardless of why the health changed.
So I went back and tested this out. I set this as a passive state and set passive state to my actor.

I copied and pasted code as is, only modifying the variable to match the actual variable in game. Just to be sure this is what I have in atm: It doesn't appear to be working and I'm not sure why. I didn't do the regen effect because I'm trying to make sure the original effect works first. So far the variable doesn't change when the character gets weak.


<Custom Battle Effect>
user.moodHit = false;
</Custom Battle Effect>

<Custom Respond Effect>
if (target.isDying() && !target.moodHit)
{
target.moodHit = true;
$gameVariables.setValue(72,
$gameVariables.value(72 + 20);
}
else if (!target.isDying() && target.moodHit)
{
target.moodHit = false;
}
</Custom Respond Effect>

EDIT : WAIT I SEE IT. parenthesis on the variable. Disregard, this part's working.

Okay, one last question then.


No, it would run the check every regeneration phase. Then if the condition is met, the code would happen, regardless of why the health changed.

If I run this a second time as a custom regenerate effect, since it doesn't care why the health changed, wouldn't going low health from regular enemy damage cause the variable to trigger twice, once for the respond effect and once for the regen effect?

Final edit: disregard this as well. Tested in both and it's working. TY!

Original thread: https://forums.rpgmakerweb.com/threads/trying-to-make-a-variable-increase-when-a-character-is-weak.166827/