Conditional expressions parser? How to manage conditions out of a C# script?

This will be more a question about programming in general, than about Unity. But! I would really be very pleased if someone shows me a way how to manage the issue.

Assuming we have a LightBulb class. It can be turned on and off. Simple.
We also have a global variables database, say, Dictionary (string, int) or something else. So we can have, i.e. 3 wood, 19 stone, killedTheOldLady = 1 (not “true”, just to simplify), and so on.

Now we want the LightBulb class to have another field. “string condition”. We can put here some expression like, hypothetically, “wood == 5 && stone == 5”.
Now when we create our database we can put there any variable we make up and the corresponding value, and we want each instance of LightBulb we created turn on if its condition was met.

For example, in the console, we could log the state of each bulb we have instantiated, or simply put some circles into the scene, that can change color depending if it’s on or off

My question is: what is the simplest way to do it?

  1. Writing my own parser/compiler of my own language is, as you can understand, very funny. I’m just a beginner. But I know I should code how the string should be interpreted
  2. Embed Lua, Python, I don’t know, JS! They all already have their own logic and I could somehow figure out how to translate from C# to a scripting language and back. This seems more simple.
  3. Parse conditions from XML, where conditions are arguments? I’ve heard about it, but is this the easiest way to do it?

Please tell me, what should I do for my stupid branching text adventure =)

Just use my logic expression parser which is an extension of my expression parser. Here’s a webgl example.

Here’s an example usage:

using B83.LogicExpressionParser;

// [ ... ]
Parser parser = new Parser();

// parse the string expression and create an expression tree for it
LogicExpression exp = parser.Parse("wood >= 5 && stone >= 5 && haveFactory");

// Each parsed expression will use the shared expression context object which was used
// when the expression was parsed. This will hold all variables.

// this is usually the same as doing:

// To evaluate the logic expression with the current variables use GetResult()
if (exp.GetResult())
    // do somwthing

Apart from logic / boolean expressions you can also parse number expressions:

NumberExpression num = parser.ParseNumber("wood * 5 + 3");

// To get the current value of the number expression use GetNumber()
double val = num.GetNumber();

Numbers are always double values. If you need / want to use float or int values you have to cast the result. Apart from setting variables to certain values you can also assign a delegate / lambda expression to a variable:

double myVariable = 42;


Now when exp is evaluated (GetResult for logic expressions, GetNumber for number expressions) it will actually execute the lambda expression whenever “someVar” is used in the expression.

Note that when parsing an expression you can provide a custom ExpressionContext for this expression only as second parameter. You can also create your own expression context object and pass it to the Parser constructir or exchange the ExpressionContext for all following parsed expressions. That way you can create multiple seperate variable contexts if needed.

You can also provide custom functions which can be used in an expression. Custom functions are registrated in the “ParsingContext”. You can add a new function by using the AddFunction method. The actual method / delegate you provide has to have the following signature:

double Method(ParameterList p)

ParameterList is an array / List like structure but less restrictive. If a certain index doesn’t exist it will simply return “0”.

If you want to know anything else you may just have a look at the source code or just post another comment with more specific topics you want me to cover.