[SOLVED] Alternating "and/or" in GetBoolean?

Based on this thread: https://textadventures.co.uk/forum/quest/topic/5943/solved-using-an-if-to-check-two-flags

I saw this:

if ((GetBoolean (paper, "cut")
and GetBoolean (paper, "folded"))
or GetBoolean (paper, "stained"))
{ /* scripts */ }

First off, is that the actual code that works?
Second, if so, I want to be able to cover four or more states or conditions in one string.
In other words:

if ((GetBoolean (thing, "1")
and GetBoolean (thing, "2"))
or GetBoolean (thing, "3"))
and GetBoolean (thing, "4"))
or GetBoolean (thing, "5"))
{ /* scripts */ }

Does that work?

In other words, after "if" checks to see if thing 1 is true, then if checks to see it things 2 OR 3 are true AND it checks to see if things 4 OR 5 are true, before giving the result?

Does that make sense?


Sorry the editor isn't letting me edit my post ("You can't post this here"). The last couple lines should read:

In other words, after "if" checks to see if thing 1 is true, then it checks to see if things 2 OR 3 are true and then, in addition, it checks to see if things 4 OR 5 are true. And then it gives the result.


You should bracket the bits that go together, so 2 OR 3 brackets together, and 4 OR 5.

if (GetBoolean (thing, "1") and (GetBoolean (thing, "2") or GetBoolean (thing, "3")) and (GetBoolean (thing, "4") or GetBoolean (thing, "5"))) 

May be clearer like this:

if (one and (two or three) and (four or five))

you can have multiple connected conditions as either: one 'if' line or multiple nested 'if' lines (see the code box below)


note that each 'CONDITION' must be a full statement:

if (FULL_STATEMENT_CONDITION LOGIC_OPERATOR FULL_STATEMENT_CONDITION LOGIC_OPERATOR FULL_STATEMENT_CONDITION) { /* scripting */ }

LOGIC_OPERATORS: 'and' and 'or'

there's also a third LOGIC_OPERATOR: negation ('not'), but it doesn't go between the conditions like you see above, instead it goes before them:

if (not FULL_STATEMENT_CONDITION LOGIC_OPERATOR not FULL_STATEMENT_CONDITION LOGIC_OPERATOR not FULL_STATEMENT_CONDITION) { /* scripting */ }

or, there's also another/alternative syntax of doing 'not' (negation) in quest: not equal: <>

FULL_STATEMENT_CONDITION: VARIABLE <> VALUE_OR_EXPRESSION

if (VARIABLE <> VALUE_OR_EXPRESSION LOGIC_OPERATOR VARIABLE <> VALUE_OR_EXPRESSION LOGIC_OPERATOR VARIABLE <> VALUE_OR_EXPRESSION) { /* scripting */ }

Attribute VARIABLE usage, full statement forms:

NAME_OF_OBJECT.NAME_OF_BOOLEAN_ATTRIBUTE // the long form is this: NAME_OF_OBJECT.NAME_OF_BOOLEAN_ATTRIBUTE = true

not NAME_OF_OBJECT.NAME_OF_BOOLEAN_ATTRIBUTE // the long form is this: not NAME_OF_OBJECT.NAME_OF_BOOLEAN_ATTRIBUTE = true // which is this: not true ===> FALSE // which is the same as this long form: NAME_OF_OBJECT.NAME_OF_BOOLEAN_ATTRIBUTE = false

NAME_OF_OBJECT.NAME_OF_NON_BOOLEAN_ATTRIBUTE OPERATOR VALUE_OR_EXPRESSION

not NAME_OF_OBJECT.NAME_OF_NON_BOOLEAN_ATTRIBUTE OPERATOR VALUE_OR_EXPRESSION

NAME_OF_OBJECT.NAME_OF_NON_BOOLEAN_ATTRIBUTE <> VALUE_OR_EXPRESSION

Variable VARIABLE usage, full statement forms:

NAME_OF_BOOLEAN_Variable // the long form is this: NAME_OF_BOOLEAN_Variable = true

not NAME_OF_BOOLEAN_Variable // the long form is this: NAME_OF_BOOLEAN_Variable = false

NAME_OF_NON_BOOLEAN_Variable OPERATOR VALUE_OR_EXPRESSION

not NAME_OF_NON_BOOLEAN_Variable OPERATOR VALUE_OR_EXPRESSION

NAME_OF_NON_BOOLEAN_Variable <> VALUE_OR_EXPRESSION


'AND' logic:

if (CONDITION_1 and CONDITION_2 and CONDITION_3) {
  msg ("this is the same as below")
}

if (CONDITION_1) {
  if (CONDITION_2) {
    if (CONDITION_3) {
      msg ("this is the same as above")
    }
  }
}

-----------

'OR' logic:

if (CONDITION_1 or CONDITION_2 or CONDITION_3) {
  msg ("this is the same as below")
}

if (CONDITION_1) {
  msg ("this is the same as above")
} else if (CONDITION_2) {
  msg ("this is the same as above")
} else if (CONDITION_3) {
  msg ("this is the same as above")
}

you need to understand how, logic / truth tables, work, for correctly using 'and' and 'or' and negation ('not' or '<>'):

https://en.wikipedia.org/wiki/Truth_table
https://en.wikipedia.org/wiki/Boolean_algebra
http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch13s02.html

using parenthesis works the same for scripting in quest as it does for math operations, parenthesis can be used to control the 'order of operations' of the scripting, for example:

(true or true) and false = (true) and false = FALSE
true or (true and false) = true or (false) = TRUE


Works like a charm. I had too many parentheses! This made it xtra clear!

if (one and (two or three) and (four or five))

Thanks!


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums