- Home /
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?
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
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.
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 =)
Answer by Bunny83 · May 19, 2019 at 12:30 AM
Just use my logic expression parser which is an extension of my expression parser. Here's a webgl example.
edit
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.
exp["wood"].Set(15);
exp["stone"].Set(15);
exp["haveFactory"].Set(true);
// this is usually the same as doing:
parser.ExpressionContext["wood"].Set(15);
parser.ExpressionContext["stone"].Set(15);
parser.ExpressionContext["haveFactory"].Set(true);
// 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;
exp["someVar"].Set(()=>myVariable);
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.
Oh, thanks a lot for this! I looked at the source code, but I was not quite qualified to understand where to start looking. Your comment explained mostly everything. But it will do for this time, much obliged!
Hi,
Awesome class @Bunny83 !
Is it correct to say this only works with numeric values?
How do I make it work with string values?
Thanks!
Your answer
Follow this Question
Related Questions
Parser Error. 1 Answer
NullReferenceException: Object reference not set to an instance of an object 1 Answer
Compiler error in the Lerpz tutorial at startup 0 Answers
enums and array indexing - weird issue, maybe compiler bug? 1 Answer
ApplicationException: Unable to find a suitable compiler 0 Answers