I am attempting to create a landscape similar to terraria, in unreal engine 5 blueprints, using hierarchical instance static mesh instances.
For some reason it is spawning multiple chunks in the same place. I have attached a screenshot of my chunk generation function, which is all executed in the player controller.
I have tried manually placing a chunk in the world and can confirm that there where no duplicates, meaning the bug is coming from the spawnNewChunksWithinRenderRange function.
Any help appreciated ![]()
Your spawn actor node is inside the for loop, so it’ll fire as many times as the for loop runs. Check the value of render range xy and make sure it’s only running once or move the spawn actor node to the completed pin of the for loop node
The branch before the spawn actor node should be stopping duplicates from spawning though?
I have tried modifying the blueprints to “addunique” to the coordinates array to prevent duplicates, and tried spawning the actors after the loop based on the coordinates, but I get the same result.
Have you tried printing out the chunk coordinates that are being added and checking for duplicates yourself?
I have, and there doesn’t seem to be any duplicates in the coords array itself, but yet it spawns multiple in one place.
I have tried modifying the blueprints to “addunique” to the coordinates array to prevent duplicates, and tried spawning the actors after the loop based on the coordinates, but I get the same result.
That’s strange because the loop guarantees unique TempX and TempZ variables, meaning unique Chunk coordinates, so 2 Chunk actors cannot have the same X and Z in one function call.
But there are 2 function calls in your event graph, one of which is preceded by a Destroy function and that’s being called on Tick.
So it might be a case of removing the wrong element from the “CurrentSpawnedChunkCoords” array without destroying its actor, and you’re just adding it again.
Can we see the destroy function?
There is a problem with your destroy function:
You should never alter the array while you’re standing in it.
If you remove an index while you’re looping through it, you end up with unexpected results;
Index 0, 1, 2, 3, 4
Item 1, 2, 3, 4, 5
On loop 3, you remove index 2 (item 3), now index 3 becomes the new index 2, and now item 4 has index 2, so you skip it entirely, and 5 has index 3 instead of 4.
It’s most likely unrelated to your problem, because it would lead to skipping chunks that need to be destroyed instead of adding duplicates, but you should fix that asap.
Anyway, The most likely reason is a race condition happening due to trying to SpawnActor on tick, which is actually slow. (Destroy actor is also slow and shouldn’t be done on Tick).
So there are 2 ways you could try adding a gate:
after the two branches return true, add a boolean like isSpawning and set it to true, and return from the loop (You’ll be back in the next tick anyway). then and at the top of the function (or preferably outside it, at the very start of Tick) you check isSpawning or is it ready to spawn another chunk. That it’s independent of runtime speed or processing power of the players’ devices.
a more complicated way of doing it would be a delayed loop with a time-out, which would be better because it’ll queue your spawns.
If that isn’t it, we’ll need a readout of the arrays and a debugging view on the actor spawns, because I think the race condition is:
SpawnActor → add to array → continue loop → SpawnActor → new tick → SpawnActor → add to array.
PS. It might also be racing the function call done on BeginPlay, so consider removing that as well, you’d doing it on the tick anyway.


