How to write booleans in damage formula

I know how to literally write out "if" and "then" and "end" and all that but, the damage formula has a 100 character limit and I've seen formulas with a format more like:

(a.atk > b.def) ? 10 : 5

And I get some of it. But, when I try to use it I always get *something* wrong somehow so I was hoping someone knew a guide on how to properly format formulas with this short-hand way?

And to be specific? How would you write out:

if(a.atk > b.atk)
b.hp -= (a.atk*2 - b.atk*2)
else 1
end

if(a.mat > b.mat)
a.mat*2 - b.atk*2
else 1
end

I need BOTH of these to happen in one damage formula.
Code:
if(a.atk > b.atk)
b.hp -= (a.atk*2 - b.atk*2)
else 1
end
What this says is ask if the user's attack is greater than their defense, t will then subtract the target's HP then return their resulting HP as the damage for the skill and will probably kill that enemy instantly. I don't know if that's the effect you want

Code:
if(a.mat > b.mat)
a.mat*2 - b.atk*2
else 1
end
This one sounds more intuitive. This would add a damage bonus equal to double the user has in magic more than the target's attack. You would write this as ((a.mat >b.mat) ? (a.mat*2-b.atk*2) : 1)
I assume you mean to write this?
Code:
if(a.atk > b.atk)
a.atk*2 - b.atk*2
else 1
end

if(a.mat > b.mat)
a.mat*2 - b.mat*2
else 1
end

What's the return value (final outcome) of both conditions?
If you need BOTH conditions to happen in one damage formula, I don't get which final result you are aiming for.

Is it the sum of both conditions?
Code:
Math.max(a.atk*2 - b.atk*2, 0) + Math.max(a.mat*2 - b.mat*2, 1)


Or the second condition overwrite the first one and took priority?
(If user magic is higher than target magic, ignore attack and use magic instead)
Code:
Math.max(Math.max(a.mat*2 - b.mat*2, 1), a.atk*2 - b.atk*2)


Or the bigger number took priority?
(If magic damage is higher than physical, use magic instead and vice versa)
Code:
Math.max(a.atk*2 - b.atk*2, a.mat*2 - b.mat*2, 1)


From what you write, I noticed the point of using condition is just to make the minimum damage can't be 0 or lower. That's why it's preferable to use Math.max function.
I assume you mean to write this?
Code:
if(a.atk > b.atk)
a.atk*2 - b.atk*2
else 1
end

if(a.mat > b.mat)
a.mat*2 - b.mat*2
else 1
end

What's the return value (final outcome) of both conditions?

Is it the addition of both?
Code:
Math.max(a.atk*2 - b.atk*2, 0) + Math.max(a.mat*2 - b.mat*2, 1)

Or the second condition overwrite the first one and took priority?
(If user magic is higher than target magic, ignore attack and use magic instead)
Code:
Math.max(Math.max(a.mat*2 - b.mat*2, 1), a.atk*2 - b.atk*2)

Or the bigger number took priority?
(If magic damage is higher than physical, use magic instead and vice versa)
Code:
Math.max(a.atk*2 - b.atk*2, a.mat*2 - b.mat*2, 1)

Anyway one of your point is the minimum damage can't be 0 or lower, that's why it's preferable to use Math.max

edit: forgot to clarify I did mean for the addition of both. I was being very convoluted with that though
Using "max" here is actually very smart and answers that for me thank you!!!

But I do still have some questions about how to format these booleans (I appreciate getting my questions answered but I want to UNDERSTAND this particular way of writing them)

(I made this formula up arbitrarily. I just want to know how it should be written)

if(a.atk > b.def)

a.atk - a.def
elsif (a.atk > b.mat)

a.atk - b.mat
else a.atk - b.atk
end

":" is "else" right? but is there a different one for "elsif"?
An elseif would be just adding another ternary within a ternary
(a.atk>b.def)?(a.ark-a-def) : ( (a.atk>b.mat)?(a.atk-b.mat) : (a.atk-b.atk) )
An elseif would be just adding another ternary within a ternary
(a.atk>b.def)?(a.ark-a-def) : ( (a.atk>b.mat)?(a.atk-b.mat) : (a.atk-b.atk) )

That's what I thought, but for some reason I always seem to get it wrong? Are the parenthesis necessary (edit: for the second "else" ternary I mean)? If so, that may be it.
If it's not working, my guess is the computer reads it as ( (a.atk>b.def)?(a.ark-a-def) : (a.atk>b.mat) ) ? (a.atk-b.mat) : (a.atk-b.atk) instead
I think I may have figured it out. Thanks everyone for your responses~ Tbh just finally learning the name of the format I was looking at is called a "ternary" means I can google this stuff too.

I think this one can be counted done for me.
Since it was not actually covered, let me say...
@ImpossibleSnail
the ? is what makes the statement a ternary operation.
the : is to separate between true and false
So $game_switches[1] ? add_state(1) : remove_state(1)

So in that statement you are asking what is switch 1?
Note that the ? need to have a space between it and the data you are asking about.
So the ternary operator is
question ? value if true : value if false
I got one more big one that popped up. This is for a really convoluted effect:

(b.element_rate(1) > 50) ?
(b.attack_element = 3) ?
$game_variables[21] = b.mp :
$game_variables[21] = b.mmp

$game_temp.reserve_common_event(7);
999;
: 1

I know this is wrong of course, it just fails. It doesn't even crash? So to explain step-by-step:

if the enemies element rate is a certain percent
check if their attack element is "3"
if so, set a variable to their mp
else their mmp

run a common event (ONLY IF THE ELEMENT RATE WAS A CERTAIN PERCENT)
insta-kill

ELSE

just do one hp who cares

fyi: the common event uses the variable and it works. I have tested that by itself.

How do I resolve running the common event and doing the 999 damage just within one side of the "else" statement? I have tried laying it out multiple ways and it's not working.
Well all of that is too long for the damage formula. how are you putting it in? there is a trick to putting the damage formula in the notebox.
If you need that I can tell you, can't remember who I got it from. the damage formula gets some text and then you can write this.
<formula: (b.element_rate(1) > 50) ? (((b.attack_element = 3) ? $game_variables[21] = b.mp : $game_variables[21] = b.mmp); $game_temp.reserve_common_event(7); 999) : 1 >
Might need some more tweaking.
When everything becomes too long, use a script editor to get it done.
For example, you could just literally write this in the damage formula
Code:
my_formula(a,b)
Then in your script editor, insert a new script, then start to write
Code:
def my_formula(a,b)
  # write absolutely everything here
end

To answer your case, it should be something like, for starter
Code:
def my_formula(a,b)
  if b.element_rate(1) > 50 
    if b.atk_elements.include?(3)
       $game_variables[21] = b.mp 
    else
       $game_variables[21] = b.mmp 
    end
  end
  
  return 999
end
I don't know the exact need of yours. But this is just the starter.
Well all of that is too long for the damage formula. how are you putting it in? there is a trick to putting the damage formula in the notebox.
If you need that I can tell you, can't remember who I got it from. the damage formula gets some text and then you can write this.
<formula: (b.element_rate(1) > 50) ? (((b.attack_element = 3) ? $game_variables[21] = b.mp : $game_variables[21] = b.mmp); $game_temp.reserve_common_event(7); 999) : 1 >
Might need some more tweaking.

When everything becomes too long, use a script editor to get it done.
For example, you could just literally write this in the damage formula
Code:
my_formula(a,b)
Then in your script editor, insert a new script, then start to write
Code:
def my_formula(a,b)
  # write absolutely everything here
end

To answer your case, it should be something like, for starter
Code:
def my_formula(a,b)
  if b.element_rate(1) > 50 
    if b.atk_elements.include?(3)
       $game_variables[21] = b.mp 
    else
       $game_variables[21] = b.mmp 
    end
  end
  
  return 999
end
I don't know the exact need of yours. But this is just the starter.

Oh yeah I know how to write formulas in the note box ( "eval $data_skills[X].note" if anyone else needs it ) but my thing right now is I just want to really learn how to properly write out these things. This one in particular was giving me an issue but it wasn't just that I needed this solved but I want to know *how* writing things like this works so I don't have to keep coming to the forums lol. I think I got it now though.

I did do some more experimenting and it looks like my big hang-up was

1) use parentheses to surround everything you want to run and
2) use ";" after every "line" or new thing you want to run within the parentheses

(b.element_rate(1) > 50) ? (((b.attack_element = 3) ? $game_variables[21] = b.mp : $game_variables[21] = b.mmp); $game_temp.reserve_common_event(7); 999) : 1

This one SHOULD be right but it's still just not working? Do you know for sure "b.attack_element" is right? It's not CRASHING so I would think it is but no matter how I mess with it I can't get it to return "true"? Is there a better way to check an opponents attack element?
There is NO b.attack_element
That is why it isn't working
And I don't know why you're persisting to use a single line for all.
Do you read my post at all?
There is NO b.attack_element
That is why it isn't working
And I don't know why you're persisting to use a single line for all.
Do you read my post at all?

My wording may have been confusing and I forgot to address what you said too, I apologize. That is a good point to use scripts... (though this one can fit in the note box and be read with the "eval" anyways)

but the core of my questions is to understand HOW to write the formulas in *this* particular way (using the ternary). It just bugs me I don't understand it and I want to know. Even if there's another way to do it, this way is very convenient imo and I wan't to have it on hand.

But anyways yeah I thought so with the attack_element. I saw someone else throw it out there but it didn't crash? Usually RPG maker crashes when it runs into stuff it doesn't recognize? So that's what confused me.

Anyways this may be a totally separate question and something I can just go digging again for. I got my answers on the ternary stuff.
But anyways yeah I thought so with the attack_element. I saw someone else throw it out there but it didn't crash? Usually RPG maker crashes when it runs into stuff it doesn't recognize? So that's what confused me.
When you write something wrong in the damage formula, it doesn't crash. It just returns 0 because they think it would be "user friendly". Because someone might think that if the game crash, the engine is buggy, which is not true. Because that could be their fault for using the wrong formula.

You have to put this in order to make it crash (my own snippet to work in my game)
Code:
#-------------------------------------------------------------------------------
# Damage formula no rescue
#-------------------------------------------------------------------------------
alias kernel_eval eval
class RPG::UsableItem::Damage
  def eval(a, b, v)
    @user = a
    begin
      result = [kernel_eval(@formula), 0].max * sign
    rescue StandardError => err
      raise "Formula: #{@formula}\n\n#{err.to_s}"
    end
    @user = nil
    return result
  end
 
  def method_missing(name, *args, &block)
    if @user && @user.respond_to?(name)
      @user.send(name, *args, &block)
    else
      super
    end
  end
end
but the core of my questions is to understand HOW to write the formulas in *this* particular way (using the ternary). It just bugs me I don't understand it and I want to know. Even if there's another way to do it, this way is very convenient imo and I wan't to have it on hand.
In that case, if you're persisting to use ternary operator, I can not help you. Maybe I could, but the end result probably an ugly code and hard to read.

----
Speaking of the attack element.
It is by accessing b.atk_elements and it is in a form of array. Not a single value. I assume you know what is Array?
(For some reason the website kind of bugged when I was trying to reply so hopefully this won't post like 100000 times when it goes up)

I don't want to upset you by saying anything, but I feel like your tone is getting very frustrated? I'm sorry if I've come off the wrong way I promise I'm not dismissing the things you say.

I really appreciate you taking the time to help me I really do. I'm just trying to learn, I'm self-taught (as you can guess) I just learn what I can where I can get it and I wanted to know more about this particular thing is all. Maybe I won't use it in the end, but I just wanted to understand something I didn't know.

I appreciate you answering my questions and I thank you for showing me a more efficient alternative. I'm sorry I rubbed you the wrong way. I do know about arrays and I've got what I need now though to keep going. I don't think I have any more questions.
I'm actually surprised that you did apologize and actually admit that you didn't dismiss my answer. I came off as a defense by being a little hostile because sometimes people just stubborn enough not to listen to other people's advice and keep saying "there is nothing wrong with this, I just need to make this thing works". To put a few examples is when people asked to post
  • A screenshot of the error, instead they just write the error, not the screenshot of it (because the information could be missing).
  • The entire window of the event (not cropped). They didn't believe other was causing it.
So I'm going to clarify once more. Which is your priority?
  • To make things work in a single line?
  • To make things work?
  • Have you managed to make things work not in a single line? (because by your wording, it seems like you have already know the more efficient way for it)
I'm still here to answer any of your questions if you have one.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-write-booleans-in-damage-formula.126831/