For Loop and While Loop - examples


I’ve been wondering how they could come in handy, but I haven’t found any places where I could use it and I kind of see the power of them. Can someone explain in what kind of circumstances would it benefit me and such.

For Loop lets you repeat a section of code a specific number of times. Say you want a machine gun to fire X bullets every time you hit fire, or if I want to place X tiles along the Y axis until I increment over on the X axis.

Then there are For Each loops where you want to run a piece of code for every member in your array. If I have 200 vectors and I want something to happen with each vector, For Each Loop is the way to go.

While Loops are good for conditional looping, such as “If X > 5, keep looping”. IE, your character regens health as long as his HP is under the max. Once it reaches max, the while loop ends.

There are really endless scenarios for all the loops and you will really need to make use of them throughout your game.

Thanks! Right now I use compare nodes to handle regen and I’ll definitely look into that!