Hi, my character has to perform random tricks from a list of tricks. For this four random tricks need to be selected from the trick list.
How to do this is clear to me. However I want to avoid that he performs the same trick twice. How can I do this? I tried for each loop but just cant wrap my head around how to do this.
You can use the random range node to get a random number between indexes of the array. Just set to limit from 0 to the array length -1.
To be sure the consecutive numbers do not repeat with earlier ones:
Create array which will hold results.
In a for loop with break:
Compare the generated random number with result array (use contains check).
If the number does not exist in result array then add it, else select another random and repeat the check (do a loop back before the random range node)
Once you have the right amount in the return array you can call break on the random loop.
I wanted to make it a more universal function so it doesn’t just limit you to tricks.
Ideally it would be written with a wildcard array function but that requires c++ which not all people use in their projects.
@ClockworkOcean Forgot about the shuffle node. Yes it’s more compact.
Work smarter not harder I guess
Edit: only downside is that shuffle seems to pass in the array by reference and will rearrange all items in it permanently.
Not sure if keeping order is crucial in this case. Perhaps shuffling a copy of the array would be a solution?