[SOLVED] Saving array into webstorage via RegexMatchAt/Mid

0 favourites
  • 9 posts
From the Asset Store
Forget about default textbox restrictions, you can create sprites atop of the textbox
  • I have several webstorage keys called in the format of GameSave[X][Y] like:









    and so on

    Now the X and Y after the "GameSave" are X and Y indices of an array.

    How do I use Regex (or the regexreplace or other similar regex expressions) to set a general expression to value of X and Y of array?

    I'm really bad at text-related expressions.

    The action for array is Set at (GameSave.CurX,GameSave.CurY) the "value"

    For example: GameSave13

    "Value" =

    Set "X" to webstorage.localvalue("GameSave" & "Insert Regex expression here for X" & "Insert Regex expression here for Y")

    "GameSave" & "Insert Regex expression here for X" & "Insert Regex expression here for Y"

    ^this should result to GameSave13 (and all other GameSave named keys)

    Anyone who can help me?


    I was just thinking of something about this:

    To simplify:

    for X

    retrieve the 1st number after "GameSave", ignore the 2nd number (which is for Y)

    for X

    retrieve the 2nd number after "GameSave" and , ignore the 1st number (which is for X)

    My indices are only up to 4 so having 2 digit X and/or Y is out of the question.

    I'm thinking if this is possible with regex?

  • I wrote a C2 regex tutorial you might wana check out, it has a capx. https://www.scirra.com/tutorials/1337/e ... onstruct-2

    Use RegexMatchAt

    X = "GameSave\d(\d)"

    Y = "GameSave(\d)\d"

    \d = match any single number

    () = capture this

  • IndieKiwi

    Thanks. I'll thoroughly read and study it later. Favorited and bookmarked!

    so is \d is only for matching single digit numbers?

    If you remember me and my previous problem (which you also helped me), my problem was saving the number from Animation names with names like Animation1, Animation2, and so on into instance variables.

    So will \d save 1 or 10 from "Animation10" ? I haven't checked that until you mentioned it now.

  • Oh yeah I remember that

    It will save 1 only. with "Animation(\d)\d". Using "\d{2}" will match both, or "\d+" will match one or more digits, which i assume you don't want to do here

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • IndieKiwi

    Sorry I went out earlier. I'm back to working on this.

    RegexMatchAt(WebStorage.LocalValue("GameSave"& ,"GameSave\d(\d)", "",1))

    ^is this corrrect for X?

    I got confused with all those commas and parentheses.

    and this for Y

    RegexMatchAt(WebStorage.LocalValue("GameSave"& ,"GameSave(\d)\d", "",1))

    this for whole expression of value?

    WebStorage.LocalValue("GameSave"& RegexMatchAt(WebStorage.LocalValue("GameSave"& ,"GameSave\d(\d)", "",1)) & RegexMatchAt(WebStorage.LocalValue("GameSave"& ,"GameSave(\d)\d", "",1)))

    blue for X

    red for Y

    is the expression correct?

  • Construct 2 doesn't really have a good capture groups functionality in its Regex implementation. If you use the global flag (g), you lose the ability to retrieve capture groups for your matches. If you omit the global flag, capture groups will count as matches, which is wrong. You can however hack your way around this limitation by reexecuting the same regex without the g flag on each match to retrieve the groups.

    A regex in this case might prove overkill, if you say your indexes are single digit, you should use mid("GameSave01", 8, 1) for X and mid("GameSave01", 9, 1) for Y.

  • sgn15 You've got X and Y mixed around.

    In the RegexMatchAt you only have 1 parameter, it needs 4.. because of the parentheses.

    WebStorage.LocalValue("GameSave"& RegexMatchAt(WebStorage.LocalValue("GameSave"), "GameSave(\d)\d", "",1) & RegexMatchAt(WebStorage.LocalValue("GameSave") , "GameSave\d(\d)", "",1))

  • Magistross


    Sorry to bother both of you again. It seems I can't get either mid or regexmatchat to work.

    general expression ---> webstorage.localvalue("GameSave" & "x index" & "y index")

    x index expression ---> GameSave_Text.Array_X

    y index expression ---> GameSave_Text.Array_Y

    "GameSave + X + Y" ---> "GameSave" & "use mid or regex for x here" & "use mid or regex for y here"


    RegexMatchAt Method:

    x index expression:

    RegexMatchAt(WebStorage.LocalValue("GameSave" & str(GameSave_Text.Array_X) & str(GameSave_Text.Array_Y) ), "GameSave(\d)\d", "",1)

    y index expression:

    RegexMatchAt(WebStorage.LocalValue("GameSave" & str(GameSave_Text.Array_X) & str(GameSave_Text.Array_Y) ), "GameSave\d(\d)", "",1)

    overall expression"

    webstorage.localvalue("GameSave" & RegexMatchAt("GameSave", "GameSave(\d)\d", "",1) & RegexMatchAt("GameSave" , "GameSave\d(\d)", "",1))


    Mid Method:

    x index expression:

    mid("GameSave" & str(GameSave_Text.Array_X) & str(GameSave_Text.Array_Y), 8, 1)

    y index expression:

    mid("GameSave" & str(GameSave_Text.Array_X) & str(GameSave_Text.Array_Y), 9, 1)

    overall expression"

    webstorage.localvalue("GameSave" & mid("GameSave" & str(GameSave_Text.Array_X) & str(GameSave_Text.Array_Y), 8, 1) & mid("GameSave" & str(GameSave_Text.Array_X) & str(GameSave_Text.Array_Y), 9, 1))

    The RegexMatchAt Method makes the first column (y index = 0) of cells blank (as in nothing is shown) and shows the rest as 0.

    The Mid Method makes all cells as 0.

    (Not counting the LoadGame_Switch condition) I'm using the same conditions (see attached pic) I used in 2 separate events (a. the reverse method, which is saving array values into webstorage and b. the text objects showing the array values). They work, so I'm pretty sure the conditions are right.

    I already checked LoadGame_Switch and it's correct too. LoadGame_Switch is meant to check if I just saved a game (save the information into both array and webstorage) or not (I will load the webstorage into array cells)

    I'm not sure on the x and y of the action.

  • I DID IT! 2 days (and nights) or trying to figure this out finally bore fruit!



    Thanks to both of you. Very very much appreciated.

    I said in my previous post that conditions were correct, and then the expressions for the value were correct (I tried them in a separate text object and they showed the correct values). The last thing that was unconfirmed to be correct were the X and Y parameters (in the screenshot I posted above)

    So I changed the X and Y to:

    X changed from GameSave_Array.CurX to GameSave_Text.Array_X

    Y changed from GameSave_Array.CurY to GameSave_Text.Array_Y

    GameSave_Array is an array object while GameSave_Text are text objects in my load game layout each with different values for Array_X and Array_Y variables.

    For those who might need to do something similar to this, here are the expressions that work for me

    Mid expression

    WebStorage.LocalValue("GameSave" & mid("GameSave" & str(GameSave_Text.Array_X) & str(GameSave_Text.Array_Y), 8, 1) & mid("GameSave" & str(GameSave_Text.Array_X) & str(GameSave_Text.Array_Y), 9, 1))

    RegexMatchAt expression

    Webstorage.localvalue("GameSave" & RegexMatchAt("GameSave" & str(GameSave_Text.Array_X) & str(GameSave_Text.Array_Y), "GameSave(\d)\d", "",1) & RegexMatchAt("GameSave" & str(GameSave_Text.Array_X) & str(GameSave_Text.Array_Y), "GameSave\d(\d)", "",1))

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