Issues with Actors overlapping when spawned in random locations!

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? ^^

hey thanks for your answer :slight_smile:
To my understanding return instantly ends the function and returns the value. So my thought process is, that i’m checking every spawn location i’ve created so far. If any one of them is within MinSpawnDistance i return false saying that i can’t let anything spawn there and end the function.
But if none of these checks return false that should indicate that no spot was within MinSpawnDistance thus making the return value true for “yes you can spawn something there”

why not just use the collision handling override on your spawn actor node. if you set it to “try to adjust location, dont spawn if still colliding” then it will try to adjust the spawn location to where theres no collision, if it doesnt find a place then it doesnt spawn the item.

i just did a test with it ny spawning more characters at my players location and it resulted in spawning 4 more characters before it stopped spawning more. that was with my character staying in one place and just repeating the spawn.

LOL well thats actually a great idea haha, thank you^^
i mean i still don’t understand why it doesn’t work my way :confused:

i will have to be careful to spawn an amount similar to what i planned in the first place, since the recursive function was there for a reason but i guess if i spawn a few more than before it should work just fine

i mean you could create your own method and script it but it just seems easier to use the built in functionality.

then again you could have created a function like in the picture below. the function i created takes in a location and checks for overlaps there, if theres a overlap we run the loop again, if not we break the loop and return the location and if its good.