# perform maths expression stored in a string?

1 favourites
• 15 posts
• I'd like to store a maths formula in a string then use it as an expression to be solved.

For example;

myStringVar = "1+1+(3/2)"

myIntVar = int(myStringVar)

Construct doesn't allow this.

Is something like this possible? Perhaps Js? I have no knowledge of how it can be done.

Thanks.

• Take a look at Functions, its pretty close to what you are asking for.

• Perhaps Js?

You can use the eval function.

• You can also write a parser that does it. Basically it pulls out the numbers and operators from the string and solve it.

This can be preferred over just running it as JavaScript for a few reasons.

1. If the string is something the player writes they could take advantage and run arbitrary code.

2. You have more control over how the text is parsed and the error messages if there is a typo.

Anyways here are a list of various examples of the parsing way of doing it.

• Those are great examples, thank a lot guys!

• In your example file you use sqr(), but that only requires 1 parameter, is it possible for it to accept 2 or 3, for say min() or lerp()?

I'm having a hard time understanding the code, especially as it uses the old function object.

Thanks.

• Which example? I've probably made too many. Depending on the example it's possible to make functions that take more than one parameter. Do you have a full list of features you want it to have? I'm guessing you want all the expressions that construct expressions have?

numbers: 22, 33, 1.66

binary operators: +-*/^

unary operators: -

parenthesis: ()

functions: sqrt(x), min(x,y), max(x,y), sin(x),...etc.

Can even make it have the the conditional operator (?:), but that can be a bit trickier.

Even though I use old functions it shouldn't be confusing. I tend to follow the pattern of making local variables which i immediately set from function.param(n), which works the same as function parameters. At any rate i tend to not use c3 if i can.

• Here is my latest example of a parser. Has error checking, and has functions like sin() and min(), but you can add more.

• Here is a library which can parse and solve math expressions:

• ## Try Construct 3

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

Construct 3 users don't see these ads
• Here is my latest example of a parser. Has error checking, and has functions like sin() and min(), but you can add more.

https://www.dropbox.com/s/ogcxmjml4560avl/expression%20parser%20oct2021.capx?dl=1

Thats exactly what i was hoping for.

You truly are a legend in your intelligence, creativity and kindness. Thank you.

Thanks also for you suggestion dop2000

• Here is my latest example of a parser. Has error checking, and has functions like sin() and min(), but you can add more.

https://www.dropbox.com/s/ogcxmjml4560avl/expression%20parser%20oct2021.capx?dl=1

I tried adding a third parameter so i could add a lerp() function but it only returns the value of the first param and not the answer.

• Hi,

I haven’t had a chance to mess with it this week. Right now it was setup for handling a max of two parameters. With more the extra values are discarded atm.

Anyways, it’s something that can be fixed. I’ll look it over this weekend.

• I'd really appreciate that.

Its the last part of the puzzle for the my current project.

• Ok, here is an updated version. You can have functions use as many parameters as you need.

• Excellent. Thanks very much.

Will try to learn from this.