How do I write if statements with complex conditions (2 or 3 conditions)?

From OHRRPGCE-Wiki
Jump to: navigation, search

Short Answer[edit]

You need to use and or or. As in:

if(condition 1, and, condition 2) then, begin ...

Long, Detailed Answer[edit]

You might ask this when you get a certain HSPEAK.EXE error message about your use of if:

ERROR: in line 5 of script main in C:\OHRRPGCE\SOURCE\CURRENT\wip\none.hss
if (key is pressed(key:1), key is pressed(key:2)) then (
if statement has 2 conditions. It should have only one. Use and and or for
complex conditions

HSPEAK is complaining that it's found something like this:

if (condition1, condition2) then, begin ...

At first glance, it appears to be the proper way to make an if statement with more than one conditional. What other way is there?

Well, much like in English (and probably other languages as well), you can't just say "If there is an orange in my fridge, the orange isn't mouldy then I will eat it". You have to use some "glue" in between to make sense of your statement. If there's an orange and it's not mouldy? If there's an orange or it's not mouldy? If there's an orange, or if it's mouldy, but not both? (ok, that's just stupid. But, you get the idea, hopefully)

In plotscripting, these glue words are known as operators. There are a number of operators:

  • and
  • or
  • xor

An operator takes the values on either side of it, and mashes them together some how to create a third value, which is known as the "result" of the operator.

This is a list of Logical operators. They implement what's called Boolean logic. That is, "If condition 1 and condition 2, then ...". They take the Boolean value of the conditions on either side of it, and return either true or false, depending on the combination of values:

False/False True/False True/True
and False False True
or False True True
xor False True False

So, now that you know about the operators, it's time for you to learn how to wield them. It's easy, really:

if (condition 1, and, condition 2) then, begin ...

That will take the boolean values of condition 1 and condition 2, and return true ONLY if they're both true.

if (condition 1, or, condition 2) then, begin ...

That will take the boolean values of condition 1 and condition 2, and return true if either of them are true.

if (condition 1, xor, condition 2) then, begin ...

That will take the boolean values of condition 1 and condition 2, and return true ONLY if one of them is true, not both.