Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by thepunkoff · May 18, 2019 at 07:13 PM · compilerconditionparser

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 =)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thepunkoff · May 19, 2019 at 01:22 PM 0
Share

Great! Is there some example how to use it?

avatar image thepunkoff · May 20, 2019 at 06:29 PM 0
Share

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!

avatar image tntic · Jun 16, 2020 at 12:06 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges