Truncating and saving text as number

Get help using Construct 2

Post » Sun Jan 04, 2015 6:19 am

I have an object with animations named like animation1, animation2, animation3, animation4, etc.

I want to save that to a number variable but removing the word "animation", meaning

animation1 will set a variable to 1
animation2 will set a variable to 2
animation3 will set a variable to 3
and so on

Is this possible to make using only 1 event? I'm not too good with text-related expressions.

Help please.
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Sun Jan 04, 2015 6:24 am

regular expressions:

"/animation(\d+)/"

This expression will return the number as a string, convert it to int


RegexMatchAt(String, Regex, Flags, Index)
Process the regular expression Regex on String with Flags, and in the list of results, return the entry at Index.


myString = "animation2"
int(RegexMatchAt(myString, "/animation(\d+)/", "", 0))
B
32
S
6
G
2
Posts: 317
Reputation: 2,801

Post » Sun Jan 04, 2015 6:50 am

@IndieKiwi

It's not working.

What did I do wrong here?

int(RegexMatchAt(Player.AnimationName, "/animation(\d+)/", "", 0))
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Sun Jan 04, 2015 7:01 am

Ok i tested this one. file also attached

int(RegexMatchAt(Player.AnimationName,"animation(\d+)", "",1))
You do not have the required permissions to view the files attached to this post.
B
32
S
6
G
2
Posts: 317
Reputation: 2,801

Post » Sun Jan 04, 2015 7:18 am

@IndieKiwi

for some reason, it doesn't work when I use the Player.AnimationName directly

any tips to solve this?
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Sun Jan 04, 2015 7:29 am

Also tried saving the anmation name into a text variable first, then referencing the text variable inside regex.

Still not working.
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Sun Jan 04, 2015 7:54 am

Bit hard to guess what exactly is wrong without seeing it. Is Player.AnimationName definitely containing the expected string ie "animiation1" all lowercase as well?
B
32
S
6
G
2
Posts: 317
Reputation: 2,801

Post » Sun Jan 04, 2015 8:13 am

Oh, finally got it to work!

Thanks for taking note of the lowercase issue.

My animations are Animation1, Animation2, and so on.

It works now. My bad. Noob mistake. Thanks again.
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Sun Jan 04, 2015 8:17 am

adding "i" to the third parameter in RegexMatchAt should ignore letter-casing (for future reference) ;)
B
32
S
6
G
2
Posts: 317
Reputation: 2,801

Post » Sun Jan 04, 2015 8:26 am

IndieKiwi wrote:adding "i" to the third parameter in RegexMatchAt should ignore letter-casing (for future reference) ;)


like this?

int(RegexMatchAt(Player.AnimationName,"animation(\d+)", "i",1))

where you read that (adding "i") btw? I did not see it from system expressions in manual just now?
B
16
S
7
Posts: 301
Reputation: 2,726

Next

Return to How do I....?

Who is online

Users browsing this forum: charliesan763 and 17 guests