For Loop question

How do I get a for loop to keep repeating from 1 to 4? Right now it stops after hitting 4. Thanks!

Edit: I need it because I’m making it place effects behind a player like a blood trail. It’s using an array so the first effect is index 1 until four. Once it hits four effects, the first one is removed and puts a new effect at your position to replace the 1 index. Something like…
12341234123. Every time it repeats a new effect is put at your location to replace like last time there was that number.

Another edit: I think I figured out the answer. I’ll try and use a loop if condition is true but every loop it adds 1 to the variable. If it’s at for it sets it to 1. The variable will be the index for the effect array

I believe a forloop uses and “end” so just try experimenting putting a “loop” or “loop if condition is true” before and after the “end”. One of them will work, I’ve done this in the past, but I don’t remember which and I can’t run Overwatch rn.

Also, keep in mind you most likely will need to use a wait!

To repeat a for loop a few times you can use the method @AlaskaWolf suggested or work with nested for loop, either excute a for loop within a for loop or iterate a for loop within a while loop, depending on what you wanna do and achieve these nested for loops either @AlaskaWolf’s suggestion or mine may require a waiting behaviour. Also its recommended to use nested for loops if you wanna lookup over multidimensional Arrrays, rather for Range based operations. Here is how a nested for loop in the Editor can look like:

For(Global.A, 0, 10, 1)
--do somethng before sub loop happens and repeat it from 0-10 times when sub 
  loop finishes
     For(Global.B, 1, 4, 1)
      --do something in the sub loop and repeat it from 1-4 times first, restart it if 
        the main loop got increased by number of steps
     End-- Leavses sub loop when Global.B is equal to 4
End-- Leaves main loop when Global.A is equal to 10
4 Likes

I don’t think the answers above answer what the OP is asking.

When using a for loop, the end index is non-inclusive. For example, if you said to start at 1 and end at 4, it would loop 1, 2, and 3. So to get it to go to 4 you just need to put 5 as the end.

This may seem strange, but the reason it does this is because of array indices starting at 0. If you were iterating over each item in an array, putting 0 as the start position and count of(array) as the end, it works nicely.

6 Likes

It’s true only to operate on Arrays, and if the OP uses Arrays, a nested for loop should only be applied when a multidimensional Array needs to be accessed, in only Range based operation where you just use the iterator index or the value that gots updated in my example the output becomes as desired and the loop you use for a value range works as intended. If the OP wants an Array lookup iterated in that sub loop of course the start Value needs to be 0, to access the value at index 0 on the first iteration round. The OP also wrote how he repeats to go from 1 to 4 again, which shows me that a For loop from 1-4 is not executed once which was the requested question and problem to solve, or may he didn’t described more what he actually wanna do with that or doesn’t know how for loops work in general.

2 Likes

Thanks for the help!