[Solved] Where to properly use the While condition?

Just started using Construct 2? Post your questions here

Post » Thu May 04, 2017 11:45 pm

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:
Image

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.
Last edited by MushroomKingdomr on Fri May 05, 2017 3:15 pm, edited 2 times in total.
B
14
S
5
G
2
Posts: 21
Reputation: 1,996

Post » Fri May 05, 2017 12:36 am

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
B
15
S
9
G
10
Posts: 34
Reputation: 6,495

Post » Fri May 05, 2017 12:56 am

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
ImageImageImage
B
71
S
21
G
231
Posts: 3,688
Reputation: 129,352

Post » Fri May 05, 2017 4:34 am

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.
Mistakes were made.
B
51
S
25
G
107
Posts: 1,581
Reputation: 60,458

Post » Fri May 05, 2017 3:12 pm

@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.)
B
14
S
5
G
2
Posts: 21
Reputation: 1,996


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest