Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Mar 30, 2021 at 12:36 AM by kayrayorulmaz for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by kayrayorulmaz · Jan 18, 2021 at 08:09 AM · mathequation

Why won't ExpressionEvaluator work with variables?

In my game, I wanted a string of equation objects (e.g. 1, +, 5, etc.) to be calculated into an answer. For example, the string "4 + 5" should produce the result 9. The numbers and operations change throughout the game. I found this function named ExpressionEvaluator, and thought it would work by placing the string variable like this:

 public int output;
 private string calcsString;
 
 void Update()
 {
     output = ExpressionEvaluator.Evaluate<int>(calcsString); 
 }

However, Unity kept on returning me this error:

 error CS0305: Using the generic type 'List<T>' requires 1 type arguments

What am I doing wrong here?

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

  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jan 18, 2021 at 09:54 AM

Well, there's a lot wrong here. First of all the error you showed is completely unrelated to the code you showed.


Second the class ExpressionEvaluator is an editor class and therefore can not be used by your game, only by editor scripts.


In your title you mention variables but in your description it looks like you have expressions with just literal values.


If you need an expression evaluator solution that works at runtime you can use my LogicExpressionParser which is an improved version of by old ExpressionParser. It's a very simply straight forward implementation. Though they support variables and custom functions. The created expression tree can be re-evaluated later when the variables have changed. Variables can be introduced as closures of actual C# variables.

Comment
Add comment · Show 4 · 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 Bunny83 · Jan 18, 2021 at 09:55 AM 0
Share

Note that those parsers only work on double values. However you can always cast the result to an integer if required.

avatar image kayrayorulmaz Bunny83 · Jan 18, 2021 at 10:14 AM 0
Share

Are there any examples for LogicExpressionParser? I have found some for ExpressionParser in the unify link you've sent me but nothing for LogicExpressionParser.

avatar image Bunny83 kayrayorulmaz · Jan 18, 2021 at 11:48 AM 0
Share

Hmm I homehow got distracted ^^ I had already searched for an older answer where I gave some examples. I've found this one.


It has been mentioned in a couple of places, though I think I should mention it once more. Be careful with unary $$anonymous$$us signs. Expressions like "5 + -3" won't work as unary $$anonymous$$us does not work this way. Unary $$anonymous$$us values have to be enclosed in brackets:

 "5 + (-3)"

Everything else should work as expected. Note that ^ is the power operator in a numerical expression but also xor in a logical expression.

Show more comments

Follow this Question

Answers Answers and Comments

120 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 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

Writing maths expressions 1 Answer

Optimising projectile continuously calculated impact point 2 Answers

Need help with easing effects which respect Time.deltaTime, MATH 0 Answers

Any Ideas on how to solve this? 1 Answer

Explosion damage script only deals damage in the first explosion and none afterwards 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