[Solved] Where to properly use the While condition?

0 favourites
  • 5 posts
  • So far, every time I've used it, it would create an infinite loop, causing the game to hang. What's worse, any tabs open at that time, including those on separate windows, would freeze, meaning I couldn't do anything on them; although I could close them though, there's a long delay between clicking the X and the tab actually closing, and in one instance just earlier, with the actual game, it wouldn't close at all. But newly opened tabs run just fine. I'm using Firefox if that helps.

    Edit: here's the problem script. Notice that the Stop Loop action is disabled:

    Here's the question: what would be proper examples to use the While condition in? I just don't understand it.

    I don't want to go through any more trial and error, and I've found searching unreliable; either too many results for me or not what I wanted.

    Please excuse me if this is a bad question; I'm new to C2, so you can expect me to make at least one newbie mistake, like potentially this one.

    Thank you for your support.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hard to say where you're going wrong if you don't provide at least a screenshot of how you have used the while event. That way you will get much better feed back on how to avoid an Infinite loop or even have a better way to do what you're trying to achieve with it suggested

  • Construct2 is already a giant while loop. The script repeats 60-times a second. You almost never need a while-loop. Run though the tutorials - they should help clarify how the engine works.

    https://www.scirra.com/tutorials/top

  • A while loop is used to repeat an action an indefinite amount of times within a single frame until an additional condition is not meet.

    You have to be careful to have a condition that will eventually (not) be met by your actions in the loop or include a stop loop action in your event, otherwise your game will freeze (the loop never finishes).

    As blackhornet mentioned, the cases you need to do such a thing should be pretty rare.

    For a specific simple example, if I were trying to remove all instances of a particular pattern of values out of an array, but I don't know how many times that pattern occurs ahead of time:

    While

    (Pattern exists in array)

    -> Delete index of pattern in array

    (Simplified for convenience)

    This event would keep running until all instances of that pattern were deleted.

    Almost always the "Repeat x times" and "For x to y" conditions should be sufficient to handle any looping logic necessary. They are also easier to use in terms of not getting stuck in infinite loops.

  • AuShadow: Thanks for noticing that; sorry for the "glitch". I'll have the picture of my While loop up shortly.

    blackhornet: and oosyrag: Thanks for letting me know. Had I looked up the advanced event features tutorial yesterday, I would have understood While the same day I was having the problem with it.

    Therefore, this problem is now solved, and will be marked as that. However, it would be nice for me to see future examples so I can get a better understanding of While. (Although I'll still have the picture up, so those visiting this thread can understand my initial problem at a glance.)

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