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!

1 Like

In the custom event called “Do Your Spawn Thing Here”, there’s a Set node for something called “TempObject”. However, when I tried it myself, I wasn’t able to create a node for TempObject in the custom event. How was this done?

It ended up like the image, but duplicates are still occurring. What could be the cause? I want to use the item taken from the array, but I’m not sure how to do that.

1 Like

I like more ClockworkOcean approach wihy shuffle

Note that the is valid index is set to 1 to prevent the last object from matching again in the next shuffle.
I haven’t tried it but I usually do similar things.

2 Likes

You’re over thinking it :slight_smile:

1 Like

Thank you. However, once all the target points have been used and items have spawned at each one, duplicates start to appear—probably because it’s entering a second loop. It seems like there are no duplicates during the first loop.

Also, I have multiple target points in the scene. Could you tell me how to get only a specific subset of them, instead of using “Get All Actors of Class” to get every single target point?

1 Like

Naturally, it will duplicate after the first loop :slight_smile:

You can tag them

then use

I know you’re probably going the other way but I wanted to explain:

The Array A and B are arrays of "Actor Object"s. Temp Object is a single variable of “Actor Object”. It’s not anything crazy or secret, it’s just like creating any other variable. :slight_smile:

1 Like