Nested Foreach loops

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
  • Does anyone know the proper method to get the new foreach loops to work while nested, so that you could for example compare all objects "A" to all objects "B" in one-to-one comparisons each? IE, O(N^2)?. The "run a foreach for two objects" does not appear to do this, my testing shows that it merely iterates through the list of object "A" and "B" on the same one dimensional path, ie object A#1 is only scoped at the same time as B#1, A#2 & B#2, etc.

    But I couldn't seem to get a foreach loop to call from inside a foreach loop. I mean you *can* call it, and it appears to properly exit the stack, commit this new loop, then return to the stack, and the new loop will be executed and its events function, but you can't scope the object in the top level foreach loop in the next level foreach loop afaik. IE if you do this code:

    Please login to see this link.

    Then on line #10, the "on loop for active object (blue) loop name "test1"" event will always return false, doing a short circuit and stopping the conditions and never scoping any blue active objects, because "test1" isn't referenced properly (at all) in this context, afaik.

    This isn't the same as fastloops, where you can easily retrieve the index of loop #1 inside of a subloop #2.
    If there is anyone who knows how to get this to work, please let me know, otherwise I'll tinker around with it, and if I can't find a method, I'll post it as a feature request. I'm just not sure if theres some method I'm supposed to use to get nested foreach loops to work, or if that functionality simply doesn't exist right now.

  • some more observations:

    If you have more instances of object "A" in a loop than object "B" and run a loop from "A" with "B" secondary, then the first few loops will reference one object of each class sequentially in their order (order of creation). However, once you run out of object "B" and still have more "A" to go, it will cycle back to the beginning of the index and reference the first object again, then second- it won't dereference to all objects, but rather remain one instance only, just one thats already been referenced on the last loop

    ex say you had 5x object "A", and 3x object "B"

    Loop index 1: A#1 + B#1
    Loop index 2: A#2 + B#2
    Loop index 3: A#3 + B#3
    Loop index 4: A#4 + B#1
    Loop index 5: A#5 + B#2

    If you run the loop as B primary, and A secondary (invoked from the B's object actions, with A as a parameter), then it will only do 3 loops total, not iterating through all instances of A.


    Also, the "run a foreachloop for two objects A & B" action seems to be functionally identical to running two foreach loops, one for A, and one for B, under the same name. Which makes me wonder how the stack frame works at all, since logically it should commit one loop, return, and commit another. So I think I'll investigate this more in depth

  • Appears that the loops simply have no stack exit whatsoever, unlike fastloops. The foreach loop will simply be invoked *after* the current line of events is done processing, instead of interrupting the stack, committing that action, then returning. This would in turn explain why two stacked foreach loops appear identical to a 2x object loop (they're invoked at the uh, same time? instead of sequentially).

    It still doesn't fully explain why nested loops wouldn't work. If you execute A, and A executes B once per loop, you are still getting A*B loops (of object B, technically (A+1)*B loops total) total, and they are occurring in the expected order (ex for 4x A and 2x B, it goes ABBABBABBABB on the stack). Since loop A is still clearly on the stack when loop B is resolving, theres no reason why loop A shouldn't be able to be referenced via conditions or functions in the expression editor, like fastloops work. Doing a "on foreach" condition below another one should properly scope objects in that loop.

    I think I'm about ready to write this up as a feature request, unless anyone has a trick I missed. But one thing- if I had to ask, how should it work? I personally don't see much use for the "run a for each for two objects" when its just iterating through both lists side by side, when it could actually be done as an O(N^2) loop that compares each one to one as a shortcut instead of doing the scoping yourself with nested loops. I also am curious how people think the stack frame should work with foreach loops- should it interrupt the frame and return, like with fast loops, or execute at the end of the event (like the current ForEach extension object available in 2.0)? Its probably a very tricky thing for clickteam to implement if I had to guess, so I'd wonder about the actual feasibility, its not a particularly major concern for me, you can always structure code around it (ie separate anything you wanted after a ForEach is invoked to be on a separate line of code).

    The inability to nest loops seems like a big omission though, so yeah, I'll write it up. Hope some of this can help people understand the new function

  • Thanks for the info Pixelthief.


    I would assume that a nested foreach loop would run like a nested fast loop.

    Maybe Clickteam can answer this, is this a bug or simply functions differently in this regard than a fast loop?

    Thanks.

    Please login to see this link.|Please login to see this link.|Please login to see this link.
    Need art for your game? I'm currently available for work.
    3D Modeler, Rigging TD, Animator for hire - Please login to see this link.
    Please login to see this link.

  • Thanks Francois!

    Please login to see this link.|Please login to see this link.|Please login to see this link.
    Need art for your game? I'm currently available for work.
    3D Modeler, Rigging TD, Animator for hire - Please login to see this link.
    Please login to see this link.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!