Need help with random object spawning, they keep spawning in same place


Hello everyone. I’m working on a project, and i need help with random object spawning. I wanted to make random spawning notes on walls with random codes. The code part works just fine, but for some reason objects spawn inside each other sometimes. I have object (BP_Note) and spawnpoint (BP_SpawnPoint) where i want objects to spawn. I followed some tutorial on youtube to do this, but it only showed how to do this for one random object, but i needed scalable amount of them, like 2, or 5. I tried to make the blueprint myself, but it doesn’t work. At first i tried For Each Loop with list of spawnpoint, which worked great, and all of the notes were spawned, but i needed to have scalable amount, so i tried it with For Loop, and it started to act weird. For some reason it was spawning notes on the same place twice or more times, and i didn’t know how to fix this. Then i tried while loop, but it also didn’t work. I tried to count all of successful array item deletes with variebles, but it always showed one less than amount of spawnpoints, so items were deleting from the array, but somehow notes still kept spawning on the same coordinates. I tried same counting with spawning, and all of the objects spawn were valid. Then i tried to delete by index, it didn’t work. Then i tried to check if it was deleting. I used node Random on my array to get object and then right after Remove node i placed Find node, and it gave me -1 about half the time, and other half the time it was actual index of array elements. I don’t know why some of them delete and some didn’t. Find was connected after remove for sure. Sorry if it’s a dumb question, I’m new to visual programming. Thanks in advance for any help

1 Like

Logic seems good, but :

Your “Random Integer In Range” here is being executed twice. Once for SpawnActor call, and again for the Remove call, with a different result for each node, so it is not removing the element you just used.
Promote the result of that Random call into a temp variable to get the desired behavior.

Also, ForLoop would be simpler here, no need to manage the counter yourself.

1 Like

I believe the whole method can be simplified to:

One thing you could add is a bounds check for that array, so we don’t Get a transform that does not exist.