How do I access a specific number in a variable?

Get help using Construct 2

Post » Thu Sep 03, 2015 5:31 pm

Hi.

I have a small problem. If I have a variable called "score" and the value is f.e "1235"

How do I collect what value the second number is? In this case 2

I know about the "mid" expression in text but can't get it to work on a number variable.
B
58
S
25
G
14
Posts: 786
Reputation: 13,288

Post » Thu Sep 03, 2015 5:33 pm

Sounds like something an array would be better at. Sorry I don't know the answer.
B
23
S
8
Posts: 115
Reputation: 2,640

Post » Thu Sep 03, 2015 5:42 pm

I use arrays in my game but this should be pretty simple. You can do it in a text by using mid(text,2,1) to collect the second letter so I hoped there was a simple way to do it in a number variable as well :)
B
58
S
25
G
14
Posts: 786
Reputation: 13,288

Post » Thu Sep 03, 2015 5:58 pm

you should try : mid(str(score),2,1)
Image Image
B
25
S
14
G
142
Posts: 881
Reputation: 74,020

Post » Thu Sep 03, 2015 6:20 pm

@MadSpy Can always count on you :D

This seems to work so progress. This is the scenario. I have a sprite that I want to set the frame to the same number as the second number in a variable. How do I do that. I can get a string but it needs to be set back to a number to be used in the set animation frame action :P
B
58
S
25
G
14
Posts: 786
Reputation: 13,288

Post » Thu Sep 03, 2015 6:28 pm

set animation frame= int(mid(str("score"),2,1)
you can add a check that your score has 2 digits. (if len(str(Score))>=2)
Image Image
B
25
S
14
G
142
Posts: 881
Reputation: 74,020

Post » Thu Sep 03, 2015 6:39 pm

@MadSpy Thank you but it won't let me use that expression when I want to set the frame. It tells me it has to be a number :/
B
58
S
25
G
14
Posts: 786
Reputation: 13,288

Post » Thu Sep 03, 2015 6:46 pm

Anonnymitet wrote:@MadSpy Thank you but it won't let me use that expression when I want to set the frame. It tells me it has to be a number :/


There's a typo in MadSpy's answer. It's missing a closing parenthesis :
set animation frame= int(mid(str(score),2,1))
B
75
S
31
G
27
Posts: 1,012
Reputation: 20,759

Post » Thu Sep 03, 2015 6:49 pm

Got it working. It was zero based :P So this did the trick int(mid(str(totaldeaths),1,1))

Thank you @MadSpy !
B
58
S
25
G
14
Posts: 786
Reputation: 13,288

Post » Thu Sep 03, 2015 6:49 pm

@Magistross Thank you. Just realized that :)
B
58
S
25
G
14
Posts: 786
Reputation: 13,288

Next

Return to How do I....?

Who is online

Users browsing this forum: R0J0hound, SnipG and 8 guests