Truncating and saving text as number

  • 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.

  • regular expressions:

    "/animation(\d+)/"

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

    [quote:3uaopvwy]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))

  • IndieKiwi

    It's not working.

    What did I do wrong here?

    int(RegexMatchAt(Player.AnimationName, "/animation(\d+)/", "", 0))

  • Ok i tested this one. file also attached

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

  • IndieKiwi

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

    any tips to solve this?

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

    Still not working.

  • 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?

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • adding "i" to the third parameter in RegexMatchAt should ignore letter-casing (for future reference)

  • 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?

  • Yeah does it work?

    I haven't used regex in construct 2 yet (or in javascript), but i use it at work almost daily in php.

    I test my expressions on: http://regexpal.com/

    And this should be useful for learning: http://www.regular-expressions.info/

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)