Possible to split a variable

Get help using Construct 2

Post » Fri Apr 28, 2017 6:20 am

Hi there i have a variable that is a number and i was wondering if i could set multiple text objects to different values in the variable.

For example is the variable is 156 would it be possible to set one text object to the 1 (hundreds value) another to the 5 (tens value) and the last one to the 6... all of the same variable.
B
23
S
11
G
4
Posts: 69
Reputation: 3,439

Post » Fri Apr 28, 2017 1:05 pm

you can make it a string (str(var)) and use the mid function to get a specific character
or
calculate the value with math: h=int(var/100), t=int((var-h*100)/10), s=var-h*100-t*10 (in this order)
Image Image Image
B
43
S
22
G
15
Posts: 383
Reputation: 12,262

Post » Fri Apr 28, 2017 1:12 pm

A general mechanism with the mid() expression.
http://www.blackhornettechnologies.com/Construct2Stuff/IntToTextN.capx
ImageImageImage
B
71
S
22
G
248
Posts: 3,761
Reputation: 138,073

Post » Fri Apr 28, 2017 5:53 pm

tokenat("text", index, separator)

text="1,5,6"

int(tokenat("text", 0, ",")&tokenat("text", 1, ",")&tokenat("text", 2, ","))
Image ImageImage
B
171
S
50
G
179
Posts: 8,392
Reputation: 113,478

Post » Fri Apr 28, 2017 6:32 pm

One more method! Math based, use modulo (%).

x=156

a=floor(x%10) = 6
b=floor((x%100)/10) = 5
c=floor((x%1000)/100) = 1

But still first method mercury mentioned is probably most straightforward.
mid(str(x),0,1)=1
mid(str(x),1,1)=5
mid(str(x),2,1)=6
Mistakes were made.
B
53
S
27
G
114
Posts: 1,699
Reputation: 64,446


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 15 guests