How do I convert string in variable into int?

Get help using Construct 2

Post » Mon Apr 13, 2015 11:21 am

I have global variables : money = 123 and STATS = "money|something|...|"
Now I want to load money from STATS with this : tokenat(STATS,0,"|"), question is how to convert "money" into 123?
B
4
Posts: 7
Reputation: 214

Post » Mon Apr 13, 2015 11:25 am

from the manual:
https://www.scirra.com/manual/126/system-expressions

int(x)
Convert the float or text x to an integer (whole number). If x is text, non-numeric characters are allowed after the number, but not before. For example int("33xx") returns 33, but int("xx33") returns 0.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Mon Apr 13, 2015 11:32 am

Okay, but is it possible to convert string("money") into value of this variable(123)?
B
4
Posts: 7
Reputation: 214

Post » Mon Apr 13, 2015 11:50 am

I guess using replace would be the expression to use:

replace(src, find, rep)
Find all occurrences of find in src and replace them with rep.

So that would be

system set Stats = replace(Stats, "money", str(money))
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Mon Apr 13, 2015 12:33 pm

Solved, LittleStain thanks for your help :)

Instead of STATS = "money|something|...|", I created function which set STATS to money&"|"&something
then tokenat(STATS,0,"|") will work
B
4
Posts: 7
Reputation: 214


Return to How do I....?

Who is online

Users browsing this forum: Elliott, Phacanu and 8 guests