Yeah, that's similar to my suggestion. The main difference is that I'm suggesting you let the user construct an expression string that looks like whatever you want. For instance using whatever mathematical symbols and syntax you want to support (sqrt, power, ...).
Then, when the user finishes constructing the expression (on a traditional calculator, that'd be when the =
button is pressed), you pass the user-defined expression to a preprocessor function that substitutes your allowed syntax with the syntax required by Godot's Expression
class.
So, for example, you might allow a user to enter this expression:
5 + 17^3 * √5
Since the expression class can't parse that, you'd need to preprocess it to look like:
5 + pow(17,3) * sqrt(5)
To do that, you'd have to search the User-defined expression string and replace the parts where you've allowed special syntax with the syntax the Expression class requires.
With that done, you'd then pass the update expression to Expression
for processing.
So, to recap:
- Let the User enter the expression, including any special syntax you want to support
- When the user is finished (presses the
=
button?)...
- Pass the expression to your preprocessor function
- Update the syntax as necessary for the
Expression
class
- Get the result
- Present the result to the User