Recreating Blueprint Function In C++ | Spawning Actors


Hi, I’m new to c++ and I want to recreate this function in c++. I Already tried to Get All Actors Of Class, but I’m stuck on the For Loop Part. Here is a screenshot of what I’ve done so far in c++. I would appriciate any help.

void AMyGM::SpawnOrbs()
{
	TArray<TObjectPtr<AActor>> MySpawnPointArray;
	UGameplayStatics::GetAllActorsOfClass(GetWorld(), ASpawnPoint::StaticClass(), MySpawnPointArray);

	for (int32 i = 0; i <= 9; ++i)
	{
		if (MySpawnPointArray.IsEmpty()) break; // Stop loop if empty.
		
		const int32 index = FMath::RandRange(0, MySpawnPointArray.Num() - 1);  // int32 rather than a reference to later avoid searching the array.

		GetWorld()->SpawnActor<AMyOrbClass>(
			MySpawnPointArray[index]->GetActorLocation(), 
			MySpawnPointArray[index]->GetActorRotation()
		);
		
		MySpawnPointArray[index]->Destroy(); // Destroy actor.
		MySpawnPointArray.RemoveAtSwap(index); // Swap to last and remove.
	}
}

Hope it helps

Thats exactly what I was looking for, thank you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.