Calculate formula from TextBox

Get help using Construct 2

Post » Sat Jan 18, 2014 5:45 am

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?Cipriux2014-01-18 06:30:25
B
78
S
21
G
23
Posts: 513
Reputation: 18,623

Post » Sat Jan 18, 2014 8:12 am

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.
B
10
S
2
G
1
Posts: 202
Reputation: 1,555

Post » Sat Jan 18, 2014 2:09 pm

@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)
B
78
S
21
G
23
Posts: 513
Reputation: 18,623

Post » Sat Jan 18, 2014 6:04 pm

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.
B
91
S
31
G
103
Posts: 5,240
Reputation: 67,766

Post » Sat Jan 18, 2014 11:17 pm

You could write a plugin that uses math.js's expression parser.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,630

Post » Sun Jan 19, 2014 5:34 pm

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 )Cipriux2014-01-19 17:36:31
B
78
S
21
G
23
Posts: 513
Reputation: 18,623

Post » Sun Jan 19, 2014 8:40 pm

[QUOTE=Cipriux] free tip: a game builder/editor made in Construct 2 [/QUOTE]


Yo dawg I heard you like construct
B
10
S
2
G
1
Posts: 202
Reputation: 1,555


Return to How do I....?

Who is online

Users browsing this forum: jasontoups, raptor0555 and 3 guests