[SOLVED] Saving array into webstorage via RegexMatchAt/Mid

Get help using Construct 2

Post » Fri Feb 06, 2015 10:38 am

I have several webstorage keys called in the format of GameSave[X][Y] like:

GameSave00
GameSave01
GameSave02
GameSave03
GameSave10
GameSave11
GameSave12
GameSave13
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?

Edit:
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?
Last edited by sgn15 on Sat Feb 07, 2015 6:03 pm, edited 2 times in total.
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Fri Feb 06, 2015 10:45 am

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
B
32
S
6
G
2
Posts: 317
Reputation: 2,801

Post » Fri Feb 06, 2015 10:54 am

@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.
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Fri Feb 06, 2015 10:59 am

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
B
32
S
6
G
2
Posts: 317
Reputation: 2,801

Post » Fri Feb 06, 2015 1:22 pm

@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?
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Fri Feb 06, 2015 6:12 pm

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.
B
62
S
26
G
24
Posts: 955
Reputation: 18,320

Post » Fri Feb 06, 2015 8:16 pm

@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))
B
32
S
6
G
2
Posts: 317
Reputation: 2,801

Post » Sat Feb 07, 2015 7:49 am

@Magistross
@IndieKiwi

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.
You do not have the required permissions to view the files attached to this post.
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Sat Feb 07, 2015 5:59 pm

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

@Magistross
@IndieKiwi
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))
B
16
S
7
Posts: 301
Reputation: 2,726


Return to How do I....?

Who is online

Users browsing this forum: Baidu [Spider], Beasthemu8, CJacobsSA, Mato, xTrollCx and 3 guests