How do I get the digits of a value from global variable.

Get help using Construct 2

Post » Mon Mar 17, 2014 1:06 am

I need to get them separetly to insert the number in the Variables Digit1, Digit2 and Digit3. However, I have no clue how to do that.

Could someone enlighten me? :D
B
56
S
21
G
3
Posts: 602
Reputation: 6,612

Post » Mon Mar 17, 2014 3:04 am

if you have a
Code: Select all
global number MyNumber = 123456


For any digit n (counting from 0) from the right the formula would be:
Code: Select all
floor(MyNumber/10^n)%10


for example:
Code: Select all
floor(MyNumber/10^0)%10  -> 6
floor(MyNumber/10^1)%10  -> 5
floor(MyNumber/10^2)%10  -> 4
... etc
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Mon Mar 17, 2014 3:28 am

@Yann


Could you please, explain me what these operators does(^, %)?I just know what floor expression does. I'm looking at the tutorials but I couldn't understand.

Thank you for your attention.
B
56
S
21
G
3
Posts: 602
Reputation: 6,612

Post » Mon Mar 17, 2014 3:36 am

^ is the exponent operator
Code: Select all
10^0 = 1
10^1 = 10
10^2 = 100
etc...
now in the formula, the / 10^n just set the digit you want to be the first digit.

% is the modulo operator
It's formal definition would be that it's the remainder of the euclidean division of two numbers
for example:
Code: Select all
5 : 3 = 1 with remainder 2

If you don't like this explanation you can also look at how it behaves according to different input to get a feel for it (after all we're not mathematicians)
for example:
Code: Select all
0%3 = 0
1%3 = 1
2%3 = 2
3%3 = 0
4%3 = 1
5%3 = 2
6%3 = 0
etc.
(see how things cycle?)
now in the formula, the % 10 just gives you the first digit of any number
Last edited by Yann on Mon Mar 17, 2014 1:58 pm, edited 1 time in total.
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Mon Mar 17, 2014 4:05 am

@Yann

I got it. Man, I love you. That's so cool!

Now i'm going to see how I'll put this into my event sheet.

Yes, I'm not a mathematician, I'm a grafic designer with an old passion to make my own game. :D

Thank you a lot, man. I'm very grateful.
B
56
S
21
G
3
Posts: 602
Reputation: 6,612


Return to How do I....?

Who is online

Users browsing this forum: Manubis, Ryan Kocourek, TheSupreme, TylerJS and 10 guests