How do I loop with regular expressions

0 favourites
  • 11 posts
From the Asset Store
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies.
  • 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.

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

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

    Thankful for any more detailed help.

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

    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?

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

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

  • 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")))

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

    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.

  • adventurist but why regex?

    maybe you can use tokenat instead... collectedFishLoop.capx

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

  • 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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Just thought I'd let everyone know I got it working with your help, so thanks once again!

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