I have an array and I want to loop through it and delete an element at a random index. I feel like this is simple but I'm missing something.
I am making a game in the Unreal Engine, My array is filled with objects and I have away to get input from the player and depending on this input I would like to loop for the amount of the input and delete the objects at random index, I hope this makes sense.
The code that works but doesn't do it randomly is:
for (int i = 0; i < input; i++)
{
Objects[i]->Destroy();
}
The code that I have tried but doesn't work:
for (int i = 0; i < input; i++)
{
int index = FMath::RandRange(0, input);
Objects[index]->Destroy();
}
Any help would be appreciated Thank you
Destroy()it would not make object and it's memory magically "disappear". It simply means object changed internal state to be destroyed. But your array also need to be rearranged and usually it is done effectively by erase-remove idiom - en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom