playMaker

Author Topic: Conditional Expression with "any string" condition  (Read 941 times)

Mupp

  • Full Member
  • ***
  • Posts: 167
Conditional Expression with "any string" condition
« on: February 05, 2021, 05:00:29 PM »
I have a Conditional Expression that checks if a string is any of the predefined options. It works as long as it only checks against one name, but as soon I add ||, it always returns true as long as the string is not null.
What is wrong?

Code: [Select]
spawned_obj_str == ("part3(Clone)" || "part7b(Clone)" || "part8(Clone)"
|| "part8b(Clone)" || "part9(Clone)")

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4002
  • Official Playmaker Support
    • LinkedIn
Re: Conditional Expression with "any string" condition
« Reply #1 on: February 06, 2021, 01:25:17 PM »
Hmmm, not sure. I'll investigate and report back.

EDIT: I think the problem is that the expression isn't correct. You can't use logical or on strings like that. What you really mean is:

spawned_obj_str == "part3(Clone)" || spawned_obj_str == "part7b(Clone)" || spawned_obj_str == "part8(Clone)" etc.

I think there is also a Regex action on the Ecosystem you could try. Although regular expressions can be tricky to write.

EDIT: A thread on the Regex action: https://hutonggames.com/playmakerforum/index.php?topic=7005.0
« Last Edit: February 06, 2021, 01:38:10 PM by Alex Chouls »

Mupp

  • Full Member
  • ***
  • Posts: 167
Re: Conditional Expression with "any string" condition
« Reply #2 on: February 06, 2021, 01:45:51 PM »
Thanks. Wonder why that doesn't work with strings though.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Conditional Expression with "any string" condition
« Reply #3 on: February 06, 2021, 02:00:05 PM »
Hi.
A String Switch would work for this as well maybe.

Mupp

  • Full Member
  • ***
  • Posts: 167
Re: Conditional Expression with "any string" condition
« Reply #4 on: February 06, 2021, 02:15:00 PM »
No, the conditions changes so I set the conditions to a string too. Think you can't do this in a good way with a switch.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4002
  • Official Playmaker Support
    • LinkedIn
Re: Conditional Expression with "any string" condition
« Reply #5 on: February 06, 2021, 02:53:50 PM »
Thanks. Wonder why that doesn't work with strings though.

Each part of a conditional expression has to evaluate as true or false.

"part3(Clone)" || "part7b(Clone)" doesn't evaluate as true or false.

If you typed the same code in C# you would get an error:
Operator || cannot be applied to operands of type 'string' and 'string'

You're filling in the intent when you read the whole expression because you know what you want to do - but the code is more literal.

I'll see if there's a way to show these kinds of errors, that you would get in a code editor, in the ConditionalExpression editor.