-2

In Unity I'm trying to make it so you enter an expression and then it outputs a value but I want to use variables like:

x = 10
"x + x * 10"

^ outputs 200

derHugo
  • 83,094
  • 9
  • 75
  • 115

2 Answers2

1

You will need to create a Mathematical expression evaluator. This is essentially an algorithm to tokenize your input string and then evaluate every single token. Just search for "Mathematical expression evaluator in c# " and you'll find what you need.Because this is a quite large project on its own, it might make sense to use a library for that. Here are some libraries:

Math-Expression-evaluator

Matheval

dragon
  • 9
  • 4
1

If you feel more adventurous you could even write your own évaluator using a parser library. This will allow specific behaviors if needed. Some libraries are :

Olivier Duhart
  • 362
  • 3
  • 14