MV Control Switch after Elemental Damage

Yo, me again!
What I am trying to do is to turn on a switch after an enemy took a certain amount of elemental damage and end the battle. Say as soon as the enemy has taken 500 HP of fire damage during the whole battle the switch ist turned on.

I tried to accomplish this via Yanflys Buffs & States Core using a Custom Respond effect, but I am not sure how to save the amount of damage taken. Another idea I would be fine is to turn the switch on after the enemy took a certain amount of elemental attacks instead of HP damage. In this case the switch would turn on after the enemy took fire damage for 10 times for example.

When the switch is turned on everything should be easily managable via battle events.


Here is what I tried for idea #2:

<Custom Respond Effect> if (target.result().hpDamage >= 0) { var elements = this.getItemElements(); while (elements.length > 0) { var element = elements.shift(); var state = 0; if (element === 11) { $gameVariables.setValue(81, $gameVariables.value(81) + 1); } if ($gameVariables.value(81) >= 2) { $gameSwitches.setValue(79, true); } } } </Custom Respond Effect>


Unfortunately I am still guessing half of the time, when I try to script something, so I'd be happy about any thoughts!
I tried to accomplish this via Yanflys Buffs & States Core using a Custom Respond effect, but I am not sure how to save the amount of damage taken.
I'm sure this is in a Tip & Trick, but it's easier to type it out than to look it up 😛

Code:
<Custom Apply Effect>
user.damTaken=0;
</Custom Apply Effect>

then in your Respond Effect you just do target.damTaken+=value;

Here is what I tried for idea #2:
It would be a helpful idea for you to try using indentation in your code. It's standard practice for a reason - it's much easier to read the flow of conditionals and loops. I'm going to add it in your quoted code below just to help make sense of it.

<Custom Respond Effect> if (target.result().hpDamage >= 0) { var elements = this.getItemElements(); while (elements.length > 0) { var element = elements.shift(); var state = 0; if (element === 11) { $gameVariables.setValue(81, $gameVariables.value(81) + 1); } if ($gameVariables.value(81) >= 2) { $gameSwitches.setValue(79, true); } } } </Custom Respond Effect>
As soon as we do that, a few things pop out - for one, you probably want that bottom if check to be inside of the other (only looking at the value of 81 and turning on the switch if this was a fire attack).

As a side note for conditionals, it's not necessary to create a code block with braces for just one command. It doesn't hurt anything, but it's unnecessary so your code might look a bit more cluttered than it needs to.

Is variable 81 initialized anyplace (set to 0)? If not, variables start off undefined, so adding 1 to undefined results in 0, not 1.

Also, there's a "state" variable that you declare in there for apparently no reason.
<Custom battle effect>
user._elementalDmgReceived = 0
</custom battle effect>

<Custom React Effect>
var element = this.item().damage.elementId;
if (this.isDamage() && element === 6) { //replace 6 with your element
target._elementalDmgReceived += value;
};
if (target._elementalDmgReceived > 500) {
BattleManager.abort(); //this ends the battle
//$gameSwitches.setValue(17, true); //replace 17 with your desired switch and remove "//".
}
<Custom React Effect>
Nice thanks to both of you 🙂 I got both version to work now.
I got both version to work now.
Has there been any issue with my code?
Has there been any issue with my code?
Oh sorry, lately I am not too active in the forums, so it takes me a long time to answer x.x
No issues thoguh. And thanks again 🙂

Original thread: https://forums.rpgmakerweb.com/threads/control-switch-after-elemental-damage.152814/