Time for action – Something
As an example of what NOT to do, let's take this code.
var int Int1; function PostBeginPlay() { Int1 = 5; While(Int1 > 0) { Int1 = 5; } }
When we run the game with this code, it will crash.
It is EXTREMELY IMPORTANT to always make sure the "while" condition will be met to avoid infinite loop crashes.
Let's take a look at the right way to use it:
var int Int1; function PostBeginPlay() { While(Int1 < 5) { 'log("Int1" @ Int1); Int1++; } }
In this case,
Int1
will keep incrementing until it reaches 5, and the While loop will exit.We could also use a statement called
break
to exit the loop:var int Int1; function PostBeginPlay() { While(Int1 < 5) { 'log("Int1" @ Int1); Int1++; if(Int1 == 3) break; } }
In this case, the loop will exit when
Int1
reaches 3 instead of continuing until it hits 5.Another statement we can use is called continue. Instead of exiting the loop...