Calculate formula from TextBox

0 favourites
  • 7 posts
From the Asset Store
Forget about default textbox restrictions, you can create sprites atop of the textbox
  • This may sound simple, but could not found a solution.

    I a have TextBox and inside of it I put a formula ...let's say: 2*3+3^2 or 10/(2*(2+3))

    How to calculate the result at runtime?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I can't find a way to do this without using a variable to return the result; Which may be fine if you're only calculating something at runtime, but what if you needed to calculate based on user input in the textbox? ;

    {Var1 = 10/(2*(2+3)) ; textbox.text = Var1}

    You can't turn the textbox string to an integer because the result will end at the first operand.

    if (textbox.text == "2+3"){int(textbox.text) = 2}

    You could maybe do some voodoo magic with tokenat and teach it the basis for pemdas but that's silly. So, I'm interested in the answer as well it seems.

  • Noncentz705 Yeah, that's what I am talking about. I guess a tokenat will do the work but this become complicated having in mind the order of operations including parentheses (power and multiplication before addition, subtraction, and inner parentheses before outer parentheses)

  • The simplest way would be to use the Browser.ExecJS() expression. Although the "^" is different in js, so that will have to be worked around. It is appealing to make your own parser so only certain syntax is allowed as ExecJS allows all of Javascript to be used. The problem is it can be tedious to implement.

    Here's the approach I would take:

    1. Get the text for the formula.

    2. Break it up into a list of tokens (numbers, symbols). Regular expressions can be used here or scanning over the text a character at a time, tokenat isn't suitable. Error checking is done here as well to look for invalid syntax.

    3. Convert the token list from infix notation (1 + 1) to reverse polish notation (1 1 +) as in that form it's simple to evaluate.

    4. evaluate.

    http://en.wikipedia.org/wiki/Reverse_Polish_notation

    There are other ways out there as the topic is pretty vast. You'll end up writing a lot of events (or code) to get something working well.

  • You could write a plugin that uses math.js's expression parser.

  • Thanks for all the suggestions. from what it seems...it's too complicated to do this with events. Unfortunately I don't have the knowledge to write a plugin, but a plugin will be a good idea. That will open new opportunities for games and apps. (free tip: a game builder/editor made in Construct 2 <img src="smileys/smiley2.gif" border="0" align="middle" /> )

  • free tip: a game builder/editor made in Construct 2 <img src="smileys/smiley2.gif" border="0" align="middle" />

    <img src="http://userserve-ak.last.fm/serve/500/50441183/Xzibit.png" border="0" />

    <font size="1">Yo dawg I heard you like construct <img src="smileys/smiley36.gif" border="0" align="middle" /></font>

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)