Can we use State: "IF State} { else" in Formula?

Hello,

Its my first thread and i hope i am in the right category?✌️

So i want to know if we can use states in if else in formulas? like for example this attack absorbs some hp while attacking under this "state", and likewise is it possible for defensive things like when you have this state and you got hit you absorb some of the damage as hp(recovery).

what do we call this? conditional branch in formula?

how exactly is the formula a sample would be much appreciated. thank you!

"RPG MAKER MV"
You have posted this in MV Support, but the info under your avatar says you are using XP. Could you just clarify which engine you are using?
Yes, with some caveats.

You can use conditionals in the damage formula, but the damage is considered to be the last calculation evaluated, so you need to sandwich it between creating the damage and then reteurning it. So for example, to restore 20% of damage dealt to the user if they're affected by state 14, the formula box would be (assuming MV and not XP):

var damage = a.atk * 4 - b.def * 2; if (a.isStateAffected(14)) { a.gainHp(Math.floor(damage * 0.2)); } damage

You can also omit the curly braces if your if block only has one line.

If you're doing an if/else, you can also use what's called a ternary if which is a bit more compact:

condition ? iftrue : iffalse

This will work fine *unless* you're using the auto battle trait in your game or have any states with a restriction that decides a character's turn without input. The reason for this is that when the engine is determining random actions for a non-player-controlled battler, it evaluates the damage formula for each possible action to see which one is "optimal" for that turn. This means that if the battler has state 14 at the time actions are being evaluated, they'll gain HP randomly even though they didn't do anything. This will be the case for any "additional" effect you put into a formula.
JavaScript:
isStateAffected(stateId)

Edit: Someone beat me to it.
Yes, with some caveats.

You can use conditionals in the damage formula, but the damage is considered to be the last calculation evaluated, so you need to sandwich it between creating the damage and then reteurning it. So for example, to restore 20% of damage dealt to the user if they're affected by state 14, the formula box would be (assuming MV and not XP):

var damage = a.atk * 4 - b.def * 2; if (a.isStateAffected(14)) { a.gainHp(Math.floor(damage * 0.2)); } damage

You can also omit the curly braces if your if block only has one line.

If you're doing an if/else, you can also use what's called a ternary if which is a bit more compact:

condition ? iftrue : iffalse

This will work fine *unless* you're using the auto battle trait in your game or have any states with a restriction that decides a character's turn without input. The reason for this is that when the engine is determining random actions for a non-player-controlled battler, it evaluates the damage formula for each possible action to see which one is "optimal" for that turn. This means that if the battler has state 14 at the time actions are being evaluated, they'll gain HP randomly even though they didn't do anything. This will be the case for any "additional" effect you put into a formula.
thank you so much will try this now and i will keep it in mind. Yes its for RMMV. Switched to MV months ago sorry for the info.

EDIT: @Trihan i tried the formula you posted it did work the hp gain but the damage returns a "0" value though, i have search the forum and read that the 2nd formula if you have 2 in it the 2nd one will work and tried it like this:

if (a.isStateAffected(stateID)) { a.gainHp(Math.floor(100 * 0.2)); 100 + a.atk * 4 - b.def * 2; }
it worked with damage and heal but thank you for this made it as basis and learned how to use Math in that part thank you, thank you all who answered.

thread can be locked now.

Original thread: https://forums.rpgmakerweb.com/threads/can-we-use-state-if-state-else-in-formula.159714/