hey buddies, i’m newbie with c++, with chatgpt help i have these codes for my .cpp file :
include “SpawnActorOnRandomVectorArray.h”
include “Engine/World.h”
include “Kismet/GameplayStatics.h”
include “CollisionShape.h”
include “Engine/EngineTypes.h”void USpawnActorOnRandomVectorArray::SpawnActorOnRandomVectorArray(const TArray<TSubclassOf>& ActorsToSpawn, const TArray& RandomVectors, TArray<AActor*>& SpawnedActors, UObject* WorldContextObject)
{
UWorld* World = GEngine->GetWorldFromContextObjectChecked(WorldContextObject);for (int32 i = 0; i < RandomVectors.Num(); i++) { const FVector& RandomVector = RandomVectors[i]; TSubclassOf<AActor> ActorClass = ActorsToSpawn[FMath::RandRange(0, ActorsToSpawn.Num() - 1)]; FTransform SpawnTransform(RandomVector); AActor* SpawnedActor = World->SpawnActor<AActor>(ActorClass, SpawnTransform);
UStaticMeshComponent* MeshComponent = Cast(SpawnedActor->GetRootComponent());
if (MeshComponent)
{
// Configure collision properties for the mesh component
MeshComponent->SetCollisionObjectType(ECC_GameTraceChannel1);
MeshComponent->SetCollisionResponseToAllChannels(ECR_Block);
}TArray<FOverlapResult> Overlaps; FCollisionQueryParams CollisionParams; CollisionParams.AddIgnoredActor(SpawnedActor); // Ignore the newly spawned actor FVector BoxExtent = FVector(150.0f, 150.0f, 150.0f); // Adjust the box extent as needed FTransform SpawnedActorTransform = SpawnedActor->GetActorTransform(); bool bOverlap = World->OverlapMultiByObjectType( Overlaps, SpawnedActor->GetActorLocation(), FQuat::Identity, FCollisionObjectQueryParams(ECC_GameTraceChannel1), // Use the same custom collision channel FCollisionShape::MakeSphere(SpawnedActor->GetSimpleCollisionRadius()), CollisionParams ); if (bOverlap) { // Destroy the overlapping actor and respawn at another random vector SpawnedActor->Destroy(); i--; // Retry spawning at the same index since we removed the actor } else { SpawnedActors.Add(SpawnedActor); } }
}
All i want is some actors spawning in level will not overlap with anything else.
those actors is not placing in level, with my gamemode they’ll spawning when gameplay running and on some random vectors input.
i’m feeling logic of these codes in .cpp file is good, but when gameplay running sometime actors spawn still overlap together.
How should i fixing this problem ?
Someone help me please.
Thanks for reading guys <3.