How to determine when for-loop is done - Printable Version +- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum) +-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html) +--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html) +---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html) +---- Thread: How to determine when for-loop is done (/thread-30128.html) Pages:
1
2
|
How to determine when for-loop is done - FlawlessHappiness - 06-16-2015 How do I determine when a for-loop is done? Or more specific, in what order does a for-loop run? Say, I have PHP Code: void Function() When this runs, it goes through 1 to 5, and if one of the local var named "Name_1" to "Name_5" are 1, it'll jump out of the function. But when is SetEntityActive called? After the for-loop has run through all the numbers? RE: How to determine when for-loop is done - Romulator - 06-16-2015 This code: Code: Module VBModule Will produce this: So it will do the For-Next loop first, then after it has finished, will run the proceeding code. While that is Vb.Net, since code is sequential, that is, every thing occurs in the order it is fed sequentially, a computer will follow instructions in the order it has been written. A For-Next Loop will repeat until the loop condition is met, then carry out the following code. This is why when you make a While loop and cause an infinity loop, the game crashes. Because it cannot end it, uses up resources trying to and fails to execute any latter code before hanging. RE: How to determine when for-loop is done - FlawlessHappiness - 06-16-2015 Right. Thank you for the explanation! Does AngelScript use "Next" or should I just write write something after the forloop? RE: How to determine when for-loop is done - Romulator - 06-16-2015 (06-16-2015, 11:24 AM)FlawlessHappiness Wrote: Right. AngelScript doesn't use "Next". "Next" is just there so VB.Net knows when to repeat the loop. AngelScript just uses the braces to determine where to start looping - as your code above does RE: How to determine when for-loop is done - FlawlessHappiness - 06-16-2015 Awesome! Will check if it works now! RE: How to determine when for-loop is done - Romulator - 06-16-2015 <delete> Browser had a brain fart P: RE: How to determine when for-loop is done - Mudbill - 06-16-2015 Although these might not exactly answer your question individually, they're useful to know. I haven't tested them all in AngelScript, but I assume they work. In Java, there's a keyword called "continue" which will skip the remaining code within a loop, but then continue the loop after. So for example you could do: PHP Code: for(int i = 1; i <= 5, i++) { It produces: Code: Step 1 There's another keyword called "break" which is similar, but it also stops the remaining iterations of the loop. Let's use the above example, but have "break" instead of "continue." It will produce this: Code: Step 1 You already know of "return" so I won't go there. But just for reference, using it would kill the "End" as well. Another keyword is "goto" but it should not be used, at least in Java. Perhaps it's usable in C. RE: How to determine when for-loop is done - FlawlessHappiness - 06-16-2015 Interesting. 'continue' skips what comes after. 'break' does the same as return, but instead of return something it just stops the loop. RE: How to determine when for-loop is done - Mudbill - 06-16-2015 Yup. Break only jumps out of the current loop, like a for-loop or while-loop. RE: How to determine when for-loop is done - TheGreatCthulhu - 06-16-2015 Yeah, continue and break are part of AngelScript specification, and I think they should work in the game (you can always try). I don't think AngelScript has goto, but even if it did, it should be avoided. Another important thing to understand about for loops (and loops in general) that people sometimes misunderstand: they don't execute over some period of time while you play - they basically execute between two frames (the whole loop, I mean - all iterations). The game works by giving control to the script at certain points in time; the script should do it's thing and return the control back to the game as fast as possible (this is usually not a problem, all the operations that you guys are used to do in scripts execute really fast). In any case, as others have said, the loop code will execute over and over again as long as the condition at the top is satisfied, and when it's not, the code will continue with the statements found after the loop. In your example, if you look at the loop header, there are 3 things in it: for(int i=1; i<=5; i++) The first one just starts the i variable to 1. The second thing is the condition. It says to repeat the loop as long as i is less then or equal to 5. The third expression is executed at the end of every loop run (when the loop's closing brace } is reached), and it just increments i by 1. So, basically, in this case the loop is done when i becomes 6. P.S. About break - it doesn't do quite the same thing as return. If return is executed within a loop, it will return (exit) from the entire function that contains it. If break is executed, it will just stop looping and skip to the next line of code under the loop. P.P.S. When I say "under" the loop, I mean under the closing } (of the loop). |