Prevent duplicate items from being generated

Hi,
I’m working on a system where I use TargetPoint actors to randomly spawn a blueprint item every 2 seconds. I already have a working setup that randomly chooses a TargetPoint and spawns the item there.

However, the problem is: sometimes the item spawns on top of an already existing one, which looks very bad. I want to prevent this from happening — once a point is used, I want to avoid spawning another item there again.

Someone gave me this idea as a hint:

First, create two arrays of TargetPoint actors (let’s call them Array A and Array B).
At BeginPlay, add all of your TargetPoints into Array A.

Every time you spawn an item, pick a random index from 0 to the last index of Array A, and use that to get a TargetPoint.

After using that TargetPoint, remove it from Array A and add it to Array B.

When Array A becomes empty, refill it using the items from Array B.

(If you only want to use each point once, then you don’t even need Array B.)

This hint sounds useful, but I’m still a beginner and I’m not sure how to set this up in Blueprints.

Can someone kindly show me how to do this step by step, or maybe even share a simple example?

Thanks so much in advance!

1 Like

Make the array, as you have, then use SHUFFLE to mix it. As you spawn each object, remove that index from the array.

When the array is empty, just make a new one :slight_smile:

Hey @e7de84f7c71249dc8a21a7d1f05899f3!

I threw something together really quickly- trying to explain array logic gets me confused as I’m trying to explain it. :stuck_out_tongue: so I figured a picture would be better. Let us know if you’re confused about anything!