So, I’m kind of new to UE4 in general.
So here is what I am trying to do.
Lets say I have an array (TArray) of ints of size 4 like the following:
A | B | C | D
I want to visit each entries only once, in a random order.
So some example of desired outcomes would be:
C | D | B | A
D | A | B | C
A | B | C | D (This would also work since it is possible to randomly visit them in order)
Currently the only way I can think of is having a separate bool array of size 4, where all elements are initialized to false. Every time an index is chosen, I’ll check if it is false, if it is, I’ll access the index and set that index to true. It is definitely not the optimal way since we could be wasting cycles. Was wondering if there would be a better way to achieve this.
I would make a copy of that array, get random entries from the copy, then remove whichever one I got randomly from it.
while(copy.Num() > 0)
int32 index = FMath::RandRange(0, copy.Num() - 1);
//Do whatever with the corresponding entry
This will always visit each one exactly once and you won’t have any wasted loops where you randomly pick an entry that has already been visited. And since you don’t care about the order of the copy you can use the Swap version of RemoveAt so it’s constant time to remove regardless of how big the array gets.