[SOLVED] Which loop is better (and how do I make the chosen loop)?

0 favourites
  • 5 posts
From the Asset Store
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies.
  • I am trying to iterate through an array and currently have nested for loops using the loop index to accomplish this. (Is this even best practice?) This works great for one pass through the array.

    However, I need to find the optimal way to iterate through the array multiple times until I process a certain value found in the cells, and keep searching until the value is no longer found. If I find the value, I would remove it from that cell but possibly add the value elsewhere in the array, thus the need to search/iterate through again.

    Here are my ideas on how to do this ...

    A) If possible, make a loop that uses a counter variable as the loopindex that I increase or decrease accordingly, this way I could check the cells in one row for the value I'm looking for, if value was found counter does not increase and I process the row again. If value is not found, then all clear and I increase the counter by 1 thus moving to the next row. This choice would let me process only one row at a time before having to repeat the search.

    B) Use my existing nested for loop inside a while loop, and just keep running the for loop with regular loop index until the value is no longer found in the array. This seems simpler to set up but would iterate through the whole array before having to start the search over again.

    I know either choice will most likely be a dog on performance but hoping to take less evil. The array is 8 x 10 max and I'll probably wind up repeating the array search anywhere from 0-20 times (if that helps when considering performance cost).

    My actual questions:

    1) Which choice is best (performance and ease to build)? Is there a better way I'm not seeing?

    2) If choice A is even possible and I use counter variable as loop index, which type of loop do I choose specifically and how do I set that loop to use my counter variable?

    3) If choice B the nested loop inside a while loop, how do I actually stop the while loop? I've found stop loop condition while researching but not sure where to add that in relation to the while loop.

    Thank You for any feedback or assistance!

  • First of all, with such a tiny array, you could probably get away with doing thousands of full scans in a single tick without noticing any sort of slowdown.

    That said, if you still want to optimize it in a way to reduce redundant checks to a minimum, we need to know a bit more about what you are actually trying to do.

    In particular, could you perhaps elaborate on this sentence : "If I find the value, I would remove it from that cell but possibly add the value elsewhere in the array".

    Is the value shifting conditionnal ? (if so, what is this condition ?)

    Where do you move the value ? (same row, column, anywhere in the array)

    Do you "set" another cell, or do you add a cell value to another ?

  • Unless you are performing this on every tick, with your array size I don't think there will be any difference in performance even if you do 1000 passes.

    If you need to scan every row and every cell for a certain value and repeat this until this value is no longer there, here is how you can do it:

  • First of all, with such a tiny array, you could probably get away with doing thousands of full scans in a single tick without noticing any sort of slowdown.

    That said, if you still want to optimize it in a way to reduce redundant checks to a minimum, we need to know a bit more about what you are actually trying to do.

    In particular, could you perhaps elaborate on this sentence : "If I find the value, I would remove it from that cell but possibly add the value elsewhere in the array".

    Is the value shifting conditionnal ? (if so, what is this condition ?)

    Where do you move the value ? (same row, column, anywhere in the array)

    Do you "set" another cell, or do you add a cell value to another ?

    Thanks for your feedback, yes you basically explained how it works, I always remove it once found then conditionally add to adjacent cells, i did get it working now (relief!)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Unless you are performing this on every tick, with your array size I don't think there will be any difference in performance even if you do 1000 passes.

    If you need to scan every row and every cell for a certain value and repeat this until this value is no longer there, here is how you can do it:

    Thank you very much, it took me about 1 minute to get it working. I added the extra condition to the while loop i already had and that did the trick. (I even added the loopindex safety check).

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