Else and Stop Loop

  • if you are looping and you want to stop the loop, you use the Stop Loop Action. I thought if you put an ELSE to that loop it will trigger if nothing passes the loop condition, but in my tests it seems Stop Loop is causing the Else to fire even though something passed the loop condition.

    Limited info in the doc:

    Is there a built-in way to know if nothing was selected in the loop? or do I need to set a bool inside the loop?

    Tagged:

  • hmmm, it would never have occurred to me to put an else after a loop.

    I just tried it out, and it seems that the else always executes - either after a stop loop, or when the loop completes naturally.

    so, I don't think it will help you determine if nothing was selected in the loop - you will have to use a bool.

  • that because else, triggers if the above condition isn't met, no matter what you put above it, else will always trigger. that's how else works it's a basic js action.

    unless you do else(condition == true/false) {do something}

    or in construct way you have to do

    in same else condition block another condition like

    ===========

    else

    someothercondition

    ===========

    but then else is redundant at that point.

    to better understand else in any use

    u basically do a 2 condition loop

    one would be that fires the first loop

    and once that fails to meet the condition

    else takes place.

    there is no way to avoid it unless you remove else entirely and replace it with the opposite condition of the above.

  • AllanR well in a thread a little while back oosyrag was helping out with a grid system.

    https://www.construct.net/en/forum/construct-3/general-discussion-7/random-board-generation-arrow-143655

    oosyrag

    The Else event will only run if the previous event did NOT run (meet conditions). So if none of the spots checked in the first loop were valid, stop loop would never have run, and the following event will run. If the first loop did execute, then the else event would not.

    The assumption there was that Else would work if a loop never found anything. I actually could have sworn it worked then. but I also recently tested it and I think the else still fires with the stop loop.

  • GeorgeZaharia but the above event does trigger, it just gets stopped prematurely with Stop Loop.

  • Maybe I was wrong. Else does always seem to run after loops no matter what. I looked at the example file I made and it looks like I didn't end up using else after a loop after all.

  • GeorgeZaharia but the above event does trigger, it just gets stopped prematurely with Stop Loop. Yet Else still executes.

    then either stop loop doesn't consider Else or the structure of conditions is wrong made.

    would help having a screenshot of your loop/else stop loop order of events.

    cause order of events top/bottom matters.

  • oosyrag no problem, I was assuming you were right because you usually are!

    GeorgeZaharia

    here is what I was testing:

    I can always do this (which works) but I just like the else

    Ashley can you confirm Else doesn't work for loops?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • looks ok .... stop loop tough stops the loop condition for which the action "stop loop" is set upon.

    else will fire no matter if you have stop loop in that method(because is still a Else valid argument, anything that isn't above being it null, error, NaN, infinity, even halted status of loop, or not the values above etc will fire Else). what you can try is.... add another blank subevent under else ... not as a child of else but like a 3rd condition on spacebar pressed ... then move the stop loop there. see if that works... but then stop loop would not have a loop to work with ... ehm ... maybe Else is not best use here ... you could try pinpoint when exactly you want the error to fire... for example

    if space was pressed

    u got a variable that its value is set to a random between of 5 and 25.

    condition 1 loop is true if matches the variable above... play this audio ... right?

    condition 2 (should replace else ) and pinpoint what would be the desired condition here, like if loopindex("testloop") total loops equal to zero matches to textcheck then trigger error.

    you can do that by saving your loop into an array and record the match, if it matches 2 3 times then you can play the audio based on array entries and if it doesn't matches the array then trigger error. is a bit convoluted for something simple like this. not sure what is the purpose of your test, like what is it meant for .. probably there is a simpler or better way of doing it and avoid the loop/else problem entirely.

  • GeorgeZaharia

    condition 2 (should replace else ) and pinpoint what would be the desired condition here, like if loopindex("testloop") total loops equal to zero matches to textcheck then trigger error.

    well no I can't do that because the code really goes to a down arrow for a UI selection..I'm looping through a table(database) to find the next item, if it finds one, you stop the loop, if it finds none it needs plays the error sound because there are no more.

    the 2nd example with the bool works fine.. I just wanted to know if I could use else. I know it doesn't make sense programtically, but the editor lets you put the else there and to me it would make sense for it to work since else is really only: "was last statement true"?

    thanks..

  • Else doesn't make any sense after a loop. I don't know what you'd expect it to do or even how you'd define what it does. A loop is a procedure, not a condition, so it's not applicable to ask if the condition was met.

  • Ashley

    Else doesn't make any sense after a loop. I don't know what you'd expect it to do or even how you'd define what it does. A loop is a procedure, not a condition, so it's not applicable to ask if the condition was met.

    well you can maybe see why one would think it might work if the loop is in a condition like my screenshot earlier in this thread. Because A) Else "runs if the previous event did not run" and B) the editor allows it

    if it's invalid the editor shouldn't allow you to add it. The Else should turn red like it does in other invalid situations.

  • OK, the next build marks that use invalid.

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