Using logical operator OR symbol | to test string variable value

0 favourites
  • 8 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • I want to use the | logical operator symbol when testing the value of a string instance variable.

    If var = "text1" | "text2"

    However, when I try this, the expected outcome doesn't work.

    I checked the documentation page and it doesn't explicitly state that you cannot use string values but only references numbers.

    Am I correct that | only checks for numerical values and not string? Or is there something else I'm doing wrong?

  • This is a common mistake. You need:

    If var = "text1" | var = "text2"

  • Oh okay. So, in the text box I would type...

    "text1" | var = "text2"

    .... Correct?

  • No, you generally need to use 'Compare two values', where the first is var = "text1" | var = "text2" and the second is just 1.

  • FYI the reason var = "text1" | "text2" doesn't work is because it is evaluated as (var = "text1") | "text2", since equality has higher precedence than 'or'. "text2" is then interpreted as a "true" value, equivalent to (var = "text1") | true. Anything or true is true, so the entire expression is equivalent to true. (Construct doesn't actually have a 'true' keyword, so this will evaluate to 1.)

    As noted you need to use var = "text1" | var = "text2", which is evaluated as (var = "text1") | (var = "text2") which does what you want.

  • Aaaah! That makes sense. I've checked and rechecked the documentation many times before to see if I wasn't misunderstanding that...

    Is it possible to update the documentation to make this detail clearer? It's always been my understanding that the evaluations all took place in the text box and the operators were essentially use-as-everything-else (if that makes any sense).

    Anyway, I get it now. Thank you both!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I added a note to the manual page on expressions which hopefully clears it up:

    Note a common mistake is to write comparison expressions like value = 1 | 2 with the intent to match value to either 1 or 2. However this doesn't work as it is actually evaluated as (value = 1) | 2, which always evaluates as true. Similarly value = (1 | 2) won't work as 1 | 2 evaluates to true, so it only tests if value is true. The correct way to test this is using value = 1 | value = 2.

  • Beautiful! Thank you, Ashley!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)