How do I loop with regular expressions

Get help using Construct 2

Post » Sat Apr 09, 2016 12:25 pm

Hi!
I'm making a platformer game where, when the player collides with a object called "fish", the fish object will set its own instance variable called "collected" to 1.
Then, at some point in the game, each fish object that has the variable collected set to 1, will be "saved" into a file with the help of a loop.

The file will look like this:
...
fishCollectedUID:8
fishCollectedUID:9
fishCollectedUID:10
fishCollectedUID:11
...

If you ever exit the level to the Menu, and then chose to continue, I need to somehow make a loop that can find each new line with the word "fishCollectedUID:" and use the number written after to set each fish on layout to invisible with that UID.

I know to use regular expressions, and I have no problem writing a expression to find the word "fishCollectedUID:" and the number. But I'm having trouble writing a loop that would go to the next line, after the one above has already been checked.
Also, the number of fishes collected are unknown, since the player picks them up as he/she pleases, before going back to menu.

For your knowledge, I also store the files content in a global variable, so I can check that with a regular expression like this: regexMatchAt(gameSaveInfo,...)

Is it possible to do in Construct 2? Would someone be able to show me an example of how I could do this?

Hopefully I'm clear enough with what I mean, and if I'm not, ask I'll try to be more specific. :)
Image
B
12
S
3
Posts: 40
Reputation: 847

Post » Sat Apr 09, 2016 12:56 pm

B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sat Apr 09, 2016 1:22 pm

Yeah I'm pretty sure I'm supposed to use a for each loop, but I have no clue how to make it work with regexMatchAt.

This is the only thing I've been able to come up with, and it doesn't work of course:
Image

I'm sure I'm just missing something that's not clear to me at the moment.

Thankful for any more detailed help. :)
Image
B
12
S
3
Posts: 40
Reputation: 847

Post » Sat Apr 09, 2016 3:13 pm

Okay, so I made another kind of loop like this:
Image

It works for the first fish object on the list, with UID 8. But it wont loop and continue on to the next line, and set the next fish to invisible.
Do I have to add to the loopindex for it to progress? loopindex+1 or something?
Image
B
12
S
3
Posts: 40
Reputation: 847

Post » Sat Apr 09, 2016 3:40 pm

If I correctly understood.. you want something like this..

Image

Here's how I did it collectedFish.capx

but I have trouble to understand where you write Uid..how you retrive data...so upload a simple capx...
B
66
S
24
G
7
Posts: 1,514
Reputation: 11,039

Post » Sat Apr 09, 2016 3:47 pm

I cant help you with regex expressions. Been there and died there.

But, may i suggest another way ?

Populate a Dictionary with all the fishes UID's that have collected set to 1.
For each fish > collected = 1 > add key str(UID) in the library

Make a key in the localstorage plugin and give it as value the Dictionary.asjson. Store it.

Call it back.

When done > Action > Dictionary > load > the value (json) for that key in the localstorage.

Dictonary > for each key > Pick fish int(key) > set sollected to 1


Update: about your last post
Its .... "i",loopindex("loop")))
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sat Apr 09, 2016 4:10 pm

@99Instances2Go thanks for your suggestion!

@korbaach Yeah that's pretty much what I'm doing. I actually figured out what's making the loop not work. It's because I misunderstood how "index" work in regexMatchAt(String, Regex, Flags, Index)
Image

In the above image, the loop is only working once, because the regexMatchAt is at the second round of the loop, looking at index 2, then the next round for index 3 and so on.
But the next line doesn't start until 17+ characters (in the file), so the next index shouldn't be 2, but 17+. The problem is, I don't know always how many characters each line will be, so I can't guess what index each loop round should have.
Image
B
12
S
3
Posts: 40
Reputation: 847

Post » Sat Apr 09, 2016 4:40 pm

@adventurist but why regex?
maybe you can use tokenat instead... collectedFishLoop.capx
B
66
S
24
G
7
Posts: 1,514
Reputation: 11,039

Post » Sat Apr 09, 2016 4:50 pm

@korbaach Ah wow, that looks so much better for this purpose. I didn't know about it. Thank you for letting me know! I shall read up about it and implement it.
Thanks for your time, it's much appreciated! :)
Image
B
12
S
3
Posts: 40
Reputation: 847

Post » Sat Apr 09, 2016 5:11 pm

Using regex for that doesn't sound like the most straight forward to me.

First, the flag need to be "ig" instead of just "i". The "g" tells it to not stop at the first one it finds.
http://www.w3schools.com/js/js_regexp.asp

So then your expression would be:
RegexMatchAt(gameSaveInfo,"fishCollectedUID:(\d+)","ig",loopindex)

But, that doesn't give you the uid number, it gives:
"fishCollectedUID:33" or something.
You still need to get the number out of it. I guess you could use another regex for that.

So it would look like this:

repeat RegexMatchCount(gameSaveInfo,"fishCollectedUID:(\d+)","ig") times
pick fish by uid int(RegexMatchAt(RegexMatchAt(gameSaveInfo,"fishCollectedUID:(\d+)","ig",loopindex),"\d+","i,0))
--- fish: set invisible
B
92
S
32
G
109
Posts: 5,291
Reputation: 70,993

Next

Return to How do I....?

Who is online

Users browsing this forum: beohs, divsyntax, tunepunk, Yahoo [Bot] and 9 guests