Adding Mesh Instances - Asynchronously/Multithreaded

Been toying around with this a bit as I want to mess about with a lot of instances, I have migrated my project to C++ and things are a bit faster - but the game thread still locks up a bit when I add a thousand instanced meshes - which makes sense.

I’ve been trying to figure out if it’s plausible to do this asynchronously either via AsyncTask or setting up a custom thread for it - but information regarding this is very sparse and most of the stuff I can find is fairly outdated.

From what I could gather, if you want to edit an actor you need to use TWeakObjectPtr, which I’m struggling very hard with to find any examples of how to properly use.

Here’s my latest feeble attempt at it;

class FChunkBuilderTask : public FNonAbandonableTask
{
public:
	TArray<UUVoxel*> VoxelData;
	AAChunk* Chunk;

	FChunkBuilderTask(TArray<UUVoxel*> InData, AAChunk* InChunk)
	{
		VoxelData = InData;
		Chunk = InChunk;
	}

	void DoWork()
	{
		//UHierarchicalInstancedStaticMeshComponent* HISMObject = NewObject<UHierarchicalInstancedStaticMeshComponent>();
		TWeakObjectPtr<AAChunk> ChunkWeak = MakeWeakObjectPtr(Chunk);

		AsyncTask(ENamedThreads::NormalThreadPriority, [&]()
		{
			for (int32 i = 0; i < VoxelData.Num(); i++)
			{
				ChunkWeak->HISM->AddInstance(FTransform(FVector(VoxelData[i]->Coordinates.X * 100.0f, VoxelData[i]->Coordinates.Y * 100.0f, 0.0f)));
			}
		});
	};

	FORCEINLINE TStatId GetStatId() const
	{
		RETURN_QUICK_DECLARE_CYCLE_STAT(FChunkBuilderTask, STATGROUP_ThreadPoolAsyncTasks);
	};
};

Which results in a Reading access violation error.

I also previously tried creating a new object, to try and replace the one on the actor. But that also didn’t work.

Could really use some advice here as this is new territory for me. :sweat_smile:

Edit:

Looking at the sauce for this component, it does seem to be rebuilding asynchronously. Yet it still locks up the game thread a wee bit. Maybe this is expected when trying to work with thousands of instances.

The performance is perfectly fine, is just the population that makes the FPS tank for half a second.

Guess I’ll start on optimizing which instances actually need to be rendered.