Hey!
I’m trying to spawn a bunch of randomized objects at random locations in an area, but of course if you truly randomize locations you run into the issue of objects spawning on top /inside eachother.
I believe I created a function that should fix that issue in theory, but somehow the actors are still spawning inside eachother.
You can ignore everything outside the red box, its just debug stuff. So my BP consists of an event with a for loop (for the amount of objects to be spawned). Each loop a function is called that returns the randomized class, location and a bool if it is allowed to spawn. This is then used to spawn an actor.
Now it gets a bit tricky. To calculate the locations and set the class i’m using 3 functions, one of which is recursive.
It’s basicly Event-> Random Class and Location → Free Spawn Location (recursive) → Is Space Empty?.
So the first one calls Free Spawn Location and passes the values to the return nodes. It uses weighted Bools to choose 1 of 3 Classes “randomly”.
Let’s get to the recursive randomized location part. I’m guessing the issue is whithin this or the next function.
I’m creating a new vector from a random unit vector multiplied by 1000 in x, y dimensions and adding the spawner location so that they spawn around my spawner. Next I’m calling Is Space Emty? to see if nothing has spawned around that new random location yet. If it returns true i’m adding the vector to my CurrentSpawnLocations array and return it, else i’m going into recursion so it generates a new random location to try. If this fails for ~15 times I’m guessing there is no free space available anymore and I won’t try again.
Lastly Is Space Empty? starts by looping through my array of Current Spawn Locations. It checks if the new random location (created above) is whithin a certain distance to any of the previous spawn locations. If it found one it will instantly return false. If it completed without finding one it will return true.
So what do you think? Am I close to a solution, or am I terribly wrong in everything I do? ^^