Nested Loops not working. Bug or not..?

Good evening...


The following code is of an Event, simply running two Loops, one inside the other. It never exits. It would appear (having traced the values with F9...) that the Outer Loop does not obey the test for exiting (my Bold...), despite its Value becoming 0 at the appropriate moment.

Spoiler
◆Control Variables:#0001 Oute_Loop = 3
◆Control Variables:#0002 Inne_Loop = 3
◆Comment:Outer Loop...
◆Loop
  ◆If:Oute_Loop = 0
    ◆Break Loop
    ◆
  :End
  ◆Comment:Inner Loop...
  ◆Loop
    ◆If:Inne_Loop = 0
      ◆Break Loop
      ◆
    :End
    ◆Control Variables:#0002 Inne_Loop -= 1
    ◆
  :Repeat Above
  ◆Control Variables:#0001 Oute_Loop -= 1
  ◆
:Repeat Above
◆Text:None, Window, Bottom
:    :Done



...I can make the Loop exit using Labels...

Spoiler
◆Control Variables:#0001 Oute_Loop = 3
◆Control Variables:#0002 Inne_Loop = 3
◆Comment:Outer Loop...
◆Loop
  ◆If:Oute_Loop = 0
    ◆Jump to Label:End_Here
    ◆Break Loop
    ◆
  :End
  ◆Comment:Inner Loop...
  ◆Loop
    ◆If:Inne_Loop = 0
      ◆Break Loop
      ◆
    :End
    ◆Control Variables:#0002 Inne_Loop -= 1
    ◆
  :Repeat Above
  ◆Control Variables:#0001 Oute_Loop -= 1
  ◆
:Repeat Above
◆Label:End_Here
◆Text:None, Window, Bottom
:    :Done



Is this the only way to get this code to work..? Is there something specific to the MV use of Loops that prevent the nesting from working correctly..? Have I overlooked something blindingly obvious..? 
gOib363.png



Thanks in advance for any light shed on this (apparent...) bug; meanwhile...


Have a nice day


Douglas
simply running two Loops, one inside the other.

This doesn't make any sense to me. You are using nested loops, but the init at the top should run the inner loop only 3 times since it is never reinited within the top loop. So, why nest them? The entire code will run 6 times. Is that what you want?... I feel confuzled. 🙁
I got it to work by adding an Else branch to the conditional branch, and putting the inner loop into that.  So it only does the inner loop if the outer loop doesn't break.  So ...

Code:
Control Variables: Outer Loop = 3
Control Variables: Inner Loop = 3
Loop
  If Outer Loop = 0
    Break Loop
  Else
    Loop
      If Inner Loop = 0
        Break Loop
      End
      Control Variables: Inner Loop -= 1
    Repeat Above
  End
  Control Variables: Outer Loop -= 1
Repeat Above
Text: Done



In addition, your inner loop is only going to loop once.  Because you set it to 3 right at the start of the event, when Outer Loop is 3, Inner Loop will count down to 0 and will stay there.  That means when Outer Loop is 2 and 1, Inner Loop won't run because it will already be 0.  I'm not sure if that's what you want to achieve - I suspect you're not showing us what you really want to do, but a very simplified version that loses the purpose of your using two loops in the first place.


Perhaps if you explain what you're trying to achieve, we can help you find a better way to do it.


I'm not sure why this is not working as you have it.  I've gone through the code and it seems to be doing the same thing as Ace did (though I haven't checked whether Ace has the same issue - maybe it does).  I'm having trouble getting breakpoints working in the developer console, so can only do limited debugging.  Just reading through the code and looking at the json contents of the event, it seems like it should work as you expect it to.
I think this is just an example, guys.


The idea there seems like the "for" command, a loop that is broken when a condition happens. He is just trying to put a simulated for inside another through events.


But the break not working for him properly qhen the loops are nested is stopping the idea.
My apologies; I neglected to specify that this was simply an illustration of a phenomenon, not an 'in-game' problem I need solving. When I programme loops of this nature, for whatever purpose, in whatever programming language, I expect them to work. This one doesn't. The Inner loop should run 3 times, then exit, allowing the Outer loop to run a second, then a third time before, in its turn, counting down to 0. This happens, but once at 0, the Outer loop test fails to trigger its associated Break. I had put Text boxes in to see how the Variables were faring, and to see if the test actually took place (it did...), but the following Break Loop was simply ignored, and the Outer loop Variable continued to decrement. I suggest that this is not 'normal' behaviour and, although one may 'trick' the system into working as required with a 'GoTo' label, this artifice should not be necessary.


This is simply a demonstration example; in a 'real life' scenario, it could be far more pernicious to detect, and could have more far-reaching consequences than this simple piece of code. If it should work, but doesn't, it should, in my view, either be fixed to work as expected, or mentioned so that no-one relies on it working when it won't.


Thanks for the interest shown so far; hoping for a solution, or, at least, an explanation.


Cheers


Douglas
It IS very strange.  I'd be interested to know if the same thing happens in Ace when the event is set up exactly as you have it.  I don't have time to test it myself.  I wish I knew why I can't set breakpoints - it happened to me once before and then stopped happening, but is back again now.  Being able to do that would let me know exactly at what point it's failing.
By pure coincidence, I've just bought VX Ace, and so was able to make this my very first Ace Project..! It does exactly the same, ignoring the 'Break Loop', but working with a 'Jump to Label'. No difference whatsoever.


Does this help at all..?
It's strange. I'm using loops inside loops and I'm not having any issues.


I don't use variables to control the loops because I use them usually at conversations, with show options, if the option is the one I want I use "break loop" and it works fine.


Sometimes i have a loop inside a loop inside another loop and it's ok. Always with an option that break the loop in each level.


the image is in spanish, sorry, the loop is called bucle

Captura de pantalla 2016-09-18 a las 9.30.56.png
@Dad3353 could you show us the event? Maybe you did something wrong?


Maybe not, but we have no idea of what you did do and gizzmito just said they can use it fine.
@Dad3353 could you show us the event?...



Certainly. This one never exits...

Spoiler
9VkDKiP.png



This one does...

Spoiler
IvaYQYf.png



I'll duplicate the Spanish code above and see if it works; back in a while...


Edit: The Spanish Loop works. In my example, it's the 'Break Loop' after the test on 'Oute_Loop = 0' that doesn't work. The test works, because the 'Jump to Label' works, but the 'Break Loop is ignored. Very strange.
Could you make it show a message with the value of the variables each loop, for either loop? \v[n]


That'll show us where the progress stops.
I did that, and the Variable 'Oute_Loop' does, indeed become 0 (which is why the 'Label' jump works...). Once it's gone past 0, it then continues to decrement into negative values, of course, and so never passes the test again. The 'normal' progress stops when the Variable 'Oute_Loop' reaches 0, the 'If' test is successful, but the 'Break Loop' is ignored as if it didn't exist. Henceforth, it's no longer 'normal', and the Variable just decrements, and so the Loop runs forever.
I see, so that means the "if" also works, even if the loop isn't broken, right?


Are you using any plugins? I kinda doubt it, since you said the same happened in Ace, but...
Exactly; the 'if' test is successful in detecting the 0 value of the Oute_Loop Variable, but the following Break Loop is ineffective. and yes, you're right; no plugins. In fact it's an 'empty' Project which I use simply for trying things out. I thought it might be a question of speed, to be 'fixed' with e 'Wait', but putting a Text box in there to display the Variable value changed nothing, so I abandoned that line of thought.


Have I broken the Internet..?  Is this proof of a parallel Universe..? Should I go back to knitting..? 
yMRFAS7.png
Unfortunately that is all the help I can give then... Maybe some plugin would fix this, but since no one seems to have had the problem before I doubt it has already been made...
@Waterguy...


Thanks, anyway, for your efforts. I already have a solution that works, in the use of the 'Jump to Label', and, now that I know there's a problem, I'd be extra careful in checking functionality. It's a shame though, if there's a 'flakey' instruction loose in the world, as it could cause problems for those counting on its working correctly.


Cheers


Douglas
I think I know what happens


loop 1


   if condition a break


   loop 2


      if condition b break


this doesn't work well. The break in the condition a acts in the loop 2 preventing it to happen and then the loop 1 continues happening


loop 1


   loop 2


      if condition b break


   if condition a break


this works fine, the break in the condition b acts in the loop 2 and the break in the condition a acts in loop 1


loop 1


   if condition a break


   if condition c loop 2


      if condition b break


this also works as it's supposed to work, because the loop 2 is inside a condition the first break acts in the loop 1 


So I think it's a bug or something that doesn't work the way it should
No, that's not how it works.  The loops and breaks have an 'indent' level, and if a break is executed, it looks for the loop/repeat for the appropriate indent level.  It does not get the break for loop 1 mixed up with the loop/repeat for loop 2.


At least it should not.  I was trying to debug that by stepping through yesterday, but I couldn't get any breakpoints to work.


It's good to know it's doing the same thing in Ace, though you've got an interesting example above - the use of choices within the loops adds an extra level of indenting and could be changing how it works.


If I can manage to make some time over the next few days, I'll look into this some more.  It's definitely not doing what it should be doing, and it seems to be happening only in certain situations.
Please put this as 'modest priority', as I'm sure there are many more pressing and 'fire-fighting' subjects posted every day. It should work, but It's not holding me up (nor anyone that I know...), and there is a 'work-around'. Yes, it would be a Good Thing for it to be resolved, but take time out to enjoy a cup of tea or an afternoon in the garden, too, please.


We wouldn't want to lose you. 
0VOmtFr.png
Dont know if this has to something with this directly, but on my MV Demo Days i expiriences that if you leave a Event Loop without


using Break Loop, can cause permanent LAG in your game. It happened while the Loop was running and i closed disabled the Eventpage with a Switch in which the extra loop was running, thatway the loop ended while he was waiting 1 frame and the eventpage closed. in ace everything is ok but in mv it starts to lag. Not sure if that got fixed with new Patch.

Original thread: https://forums.rpgmakerweb.com/threads/nested-loops-not-working-bug-or-not.68470/