Avoiding duplicate spawns in random item placement


I

I’m using Target Points to spawn a Blueprint item at random locations every 2 seconds. The system picks a random Target Point and spawns the item there.

However, I’m facing an issue: sometimes two items spawn at the same location, overlapping each other. This looks really awkward and messy in the game.

Is there a good way to prevent items from spawning at the same spot if an item is already there?

When it selects the random spawn point, call “get overlapping actors”, if it comes back overlapping an actor of the spawned blueprint class, loop back and select a different random point

I suggest you keep track of the spawn points that were already used. Instead of building a temporary array every time SpawnRandom is called, can you make an array variable as part of your Blueprint?
Then, when a point is used, your can remove it from the array.

Instead of using a custom event for your timer, you can use a function. That will allow you to make RandIndex a local variable of this function, which isn’t possible with custom events. Take a look at Set Timer By Function Name.

This setup lets you add new spawn points to the array at any time, if you need to (not limited to BeginPlay). You can even re-add the already used spawn points later, if you want. You can keep track of the used spawn points like so (this is the same as above, in a function, but with an extra array holding the spawn points that were used):

2 Likes

Thank you very much.
I followed your instructions and tried to set it up like the image example you shared.
However, the item (gimmick) is now only spawning in one place in the world, not at the TargetPoints I set. It keeps spawning repeatedly in the same single location.

I’m really sorry — even though you explained it so kindly and clearly, I still couldn’t get it to work properly.
Since I’m a beginner, it’s very difficult for me to figure out how to fix this issue in the Blueprint.

I would really appreciate it if someone could help me understand what might be wrong, or how to correct the Blueprint setup.

So there is something broken in my screenshots, apologies.
The Random node should not be used like that. In my screenshot, I’m saving the random index (green output pin) to a variable but then I’m using the random array element (blue output pin) of the same Random node. The Random node is evaluated twice and so has a (great) risk of picking 2 different random elements. This is because Unreal will recook the Random node every time it is needed.

If you also used the Random node, it might be why you are having issues.

Instead, you can do something like this:

Or like this:

Something else that is ‘bad’ in my first screenshot (from the previous message) is that you should first check if that the array has any element in it. If you keep removing spawn points after they are used, you will end up with an empty array so you should first add a branch checking if the array lengh is > 0.

Other than that, I don’t know why this wouldn’t work. Feel free to share a screenshot of your most recent setup.

1 Like