The title is very weird, because I don’t know how to describe my problem in one sentence, so I’ll just describe the situation.

So, imagine I have a list of scriptable objects U = [a, b, c], all of them containing bools and a flag pf wether or not the value should be inverted, and a boolean expression I need them to do, “a.val || (!b.val && c.val)”. The expression is not hard-coded anywhere, all I have is the list and the expression i need it to do. Now imagine I have another list [d, e, f, g] and a different expression “(!d.val || !e.val) || (f.val && g.val)” and I need to get its result with no hardcoded functions.

Is there a way to generalize a function to do all possible boolean expressions like that? If, for example, the only expressions I needed to do was consecuteve ANDs, then i could just make a for loop, where some value continuously adds all the values together, like “result = result && U”, same with OR operations. But the problem here is brackets and the order of AND and OR operations in general, which complicate the order in which the bools are checked.

Is there a way to input any boolean expression and solve it like that?

Uhh… I’m not 100% sure I got what you’re even asking. But, if you have a common function, then you should be able to still do loops. But I’m not sure what your common function is since I’m not sure what all your possible combinations are.

Can you better explain what you’re trying to accomplish? What is the purpose of the OR and AND checks? How are you determining what is to be used in the comparison?

Perhaps someone else will get what you want to accomplish better, but usually to generalize something, you’ll need some sort of pattern.

The 1000-foot-view answer to this is you make an expression tree and evaluate it. Your question was pretty vague about what format your “expression” was in, but presumably you’ll need a step where you “parse” your expression into an expression tree which you can then evaluate.

Thanks dude, binary expressions are just what I’ve been looking for! Now I just have to figure out how to implement them. I want to make a custom editor, where you make those boolean expressions, which are then parsed into binary trees and then cached to be used in the game like regular IF statements.

Well, if you’re looking for a logic expression parser, you could use my LogicExpressionParser which is a rewrite of my old ExpressionParser (which could only do math expressions). I actually extended my old parser because of the LSystem I’ve written as a response to this question.

So you can do:

```
var parser = new B83.LogicExpressionParser.Parser();
var exp = parser.Parse("(!d || !e) || (f && g)");
exp["d"].Set(()=>d.val);
exp["e"].Set(()=>e.val);
exp["f"].Set(()=>f.val);
exp["g"].Set(()=>g.val);
bool result = exp.GetResult();
```

Note: each expression has an expression context and a parsing context related to it. An expression context can be shared by multiple expressions. Be default a Parser instance would use the same expression context for each expression you parse. That means, setting the variables once is enough for all expressions.

Also an expression variable can be directly set to a value with the Set method or, like I did in this example, pass a delegate / method that should be invoked in order to get the value. That means in this case whenever you re-evaluate the expression tree (by calling GetResult), all the used variables would be read again.

So the Parser instance can be cached and reused when a new expression should be parsed. Variables set in the expression context will remain.

In addition you can add custom function to the parser. Those custom methods can have as many arguments as you want. Though the argument types are always double values. The result also has to be a double. Boolean values are automatically converted to / from double inside expressions. Anything not 0 is considered true.

Of course a logic expression can also involve numeric values. All the usual comparision operators are supported.