Logic to check for duplicates when instancing static meshes

I’m instancing a few thousand Static Mesh characters selected randomly from an array of just 10 different actors.
Everything works properly but occassionaly I have duplicates or twins whereby 2 of the same characters are instanced next to each other.
Wihtout sharing actual BP I was wondering if any BP experts could describe the logic required to prevent these duplicates from happening?

For each instance:

  1. Pick a point
  2. Find the already-placed item that is the closest (or the two closest) to this point
  3. Pick a new item from the list of 10
  4. If the item matches that/those from 2) then goto 3

The “find the nearest already-placed item” you can probably use a hash grid, or you can do a find-actors query and check each one of them (this will be O-n-squared, so slow after N gets big.) You can also limit how far you look for “the nearest” thing – if your full range is 200 meters, it might be OK to just look within 10 meters for the “nearest” thing, and if nothing’s there, it’s clear. This also cuts down on the N-squared factor.

Thanks. I was wondering if the logic could happen BEFORE the static mesh characters were instanced? Perhaps by holding a referene to the most recent and checking aginst that or?

The logic I propose happens before the next one is instanced, which sounds similar to “remember the previous one?”

If you need to decide the position of all of them, you can simply change “make an instance” into “add a struct in an array” where the struct contains the mesh-to-pick and the center-to-place-at.
Then, to find “nearby” meshes, search through the array instead of using world queries.
Once you decided all the locations, go through the array and spawn all the applicable meshes at the given positions.

You can’t just remember the “previous” one alone, because, if positions are random, the previous one might be far away, but something spawned a dozen items before, may be close, and may be of the same kind.

1 Like

Thanks. The ‘previous one’ logic is only going to work if spawned positions aren’t random and instead are sequential. Will work on that.