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
42
S
22
G
15
Posts: 375
Reputation: 12,221

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
21
G
231
Posts: 3,688
Reputation: 129,352

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
169
S
50
G
169
Posts: 8,286
Reputation: 108,216

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
51
S
25
G
107
Posts: 1,581
Reputation: 60,458


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, Kermad, Nicolas951, tarek2, Yahoo [Bot] and 7 guests