0

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

11
  • Please be more specific than "doesn't work". Commented Jan 15, 2021 at 17:52
  • specifically tell us what "doesn't work" mean? Commented Jan 15, 2021 at 17:54
  • Also if you destroy them randomly you could destroy one item 2 or more times! is your code OK with this? Commented Jan 15, 2021 at 17:56
  • Most probably the problem is that you do not understand how it works. When you call 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 Commented Jan 15, 2021 at 18:04
  • So the array size is 10, if the user inputs 1, then I want to loop through 9 times and delete them in a random order, so that each time the code gets ran the remaining objects are different every time. The code that is doing the random deleting is getting looped through the required amount but not deleting the correct amount. So @AKL I think your right I don't want my code to being destroying one item 2 or more times Commented Jan 15, 2021 at 18:06

3 Answers 3

0
 int num = (rand() % 
           (upper - lower + 1)) + lower; 

Here lower is your lower range and upper is your upper range for random number generation. It will give you random no.

Then Store the random no in array and check if the next random no is the same or different if found same then add a Goto statement to again get a random no.I hope it might help

Sign up to request clarification or add additional context in comments.

4 Comments

Why would you need to store index in an array or anywhere for that matter?
@RolandasUlevicius if in a loop a random no comes more than once then the function will try to delete that but eventually give an error as it is already deleted that's why.
So if I had an array with 1000000 elements your suggestion would be efficient? and the index generation could be just this v1 = rand() % 100; // v1 in the range 0 to 99 exchanging 100 to array length .
@RolandasUlevicius That won't be a big problem as it would add constant time and space. By storing the random no in its index. for example, I got 7 and storing 7 in index 7 and if I again get 7 then go and again find 2nd random no. But instead of using vectors, it will be better, I accept.
0

Better way to do this is by suing std::vector instead of normal array. In this way you can also remove the object you are destroying.

Imagine your objects are inside a std::vector called ovector, then your code will be like this:

#include <vector>
.
.
.
std::vector<objects type> ovector;

Please change object type by the type of your objects. Then fill the vector by your objects with ovector.push_back(objects[...]) one by one , and then your loop will be like:

.
.
.
for (int i = 0; i < input; i++)
    {
        int index = FMath::RandRange(0, ovector.size());
        (ovector[index])->Destroy();
        ovector.erase(index);
    }

With this method you will never destroy an object twice and also you will destroy objects by the amount of input. But first make sure that the input is not higher than number of objects!

You can also change the FMath::RandRange function with rand() % ovector.size() if it doesn't work.

1 Comment

It doens't like .erase, it's giving an error: no instance of overloaded function "std::vector<_Ty, _Alloc>::erase [with _Ty=AActor *, _Alloc=std::allocator<AActor *>]" matches the argument list. So I tried just using the unreal TArray but it says array index out of bounds
0

Hi Thank you everyone for helping, I figured out a workaround by saving the locations of all of the objects then after removing some of the object I set the locations of the objects to random places so it gives the illusion that it is getting destroyed randomly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.