How do I use the contents of a string as a calculation?

Get help using Construct 2

Post » Fri Jun 10, 2016 12:00 pm

Hi all!

Is it possible to have the contents of a string variable used as a calculation? For example if I had a string that read "1+3+4" is there any way that string could be processed to express "8"?

My string is generated through a combination of RegexReplace and replace scripts (
Code: Select all
replace(RegexReplace(testObject.inputVarOne,"([[a-zA-Z][a-zA-Z0-9]*,?)","g",""),",","+")
) eventually turning a string of values into something more akin to a mathematical formula like the one shown above. Is there a way to turn that outputted string value into a calculation that construct2 can read 8 instead of 1+3+4?

---- update

Here is what I have attempted so far:
I have created a string variable and set it to the above formula, meaning the string value is literally "1+2+3" I created a second, number variable and set it to the test variable I just made, and unfortunately instead of outputting "6" as expected, it simply output "1". Can anyone offer any insight?
B
5
Posts: 29
Reputation: 733

Post » Fri Jun 10, 2016 11:36 pm

for your particular example, you will do:

local static var sum = 0
For
>> from 0 to tokencount("1+2+4","+")
>>>> Add to: sum: int(tokenat("1+2+4",loopindex,"+")
https://www.ravenheart.ca/home
I don't check the forums much anymore, but I will receive an email for PMs.

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
23
S
6
G
1
Posts: 1,419
Reputation: 4,857

Post » Mon Jun 13, 2016 11:30 am

gumshoe2029 wrote:for your particular example, you will do:

local static var sum = 0
For
>> from 0 to tokencount("1+2+4","+")
>>>> Add to: sum: int(tokenat("1+2+4",loopindex,"+")


Thank you! This worked perfectly
B
5
Posts: 29
Reputation: 733


Return to How do I....?

Who is online

Users browsing this forum: roracle and 7 guests