How to drop an actor on the ground when spawned in C++?

Hi, I want the actors to be dropped on the ground when they are spawned in the level.
using this blueprint I can do it without any problem but how to do this using C++?
Here I am doing this by object collision channel for the ground which blocks anything.

Thank You

So, the only thing you need to do, is find a C++ equivalent of these BP functions. The LineTraceForObjects in C++ is inside Kismet/KismetSystemLibrary.h and called UKismetSystemLibrary::LineTraceSingleForObjects (UKismetSystemLibrary::LineTraceSingleForObjects | Unreal Engine Documentation). SetActorLocation and GetActorLocation have same names in C++, MakeArray is TArray<TEnumAsByte<EObjectTypeQuery>> Arr = {your channels}. Others are simple struct member access and math transformations.
The only difficulty is with EObjectTypeQuery type - you can convert ECollisionChannel to EObjectTypeQuery using UEngineTypes::ConvertToObjectType from Engine/EngineTypes.h (UEngineTypes::ConvertToObjectType | Unreal Engine Documentation).

1 Like

Thank You Sir for the detailed information, it really helps me to solve the issue :slightly_smiling_face: I will try my best to translate this blueprint to C++

I solved the Issue, but I have one problem, I am unable to create/access my own collision channel by the name Land , and after all everything works using the channel WorldStatic while it blocks everything too. But I want to use my own channel.

Working Example:

void AMyTestClass::DropItemOnGround()
{
	FVector Location = GetActorLocation();
	FVector Start = Location;

	BreakVector_X = GetActorLocation().X;
	BreakVector_Y = GetActorLocation().Y;
	BreakVector_Z = GetActorLocation().Z;

	FVector End = FVector(BreakVector_X, BreakVector_Y, BreakVector_Z + -1000.000000);
	FCollisionQueryParams TraceParams;
	GetWorld()->LineTraceSingleByChannel(OutHit, Start, End, ECC_WorldStatic, TraceParams);
	UGameplayStatics::BreakHitResult
	(
		/*HitResult*/ OutHit,
		/*out*/ bBlockingHit, 
		/*out*/ bInitialOverlap, 
		/*out*/ Time, 
		/*out*/ Distance, 
		/*out*/ Location, 
		/*out*/ ImpactPoint, 
		/*out*/ Normal, 
		/*out*/ ImpactNormal, 
		/*out*/ PhysMat, 
		/*out*/ HitActor, 
		/*out*/ HitComponent, 
		/*out*/ HitBoneName, 
		/*out*/ HitItem, 
		/*out*/ FaceIndex, 
		/*out*/ TraceStart, 
		/*out*/ TraceEnd
	);
	SetActorLocation(Location);
}
2 Likes

Your own channel is one of ECC_GameTraceChannelN (N is a number), and N is similar to order you added your collisions (for example, if your collision is the only custom channel, N = 1, youe channel is editor is ECC_GameTraceChannel1 in C++, if you add it as a second custom channel - it’ll be a ECC_GameTraceChannel2). You can create macro #define MyTraceChannelName ECC_GameTraceChannelN and use it instead of ECC_GameTraceChannelN if you want.
Also, does this code compiles? For example:

UGameplayStatics::BreakHitResult
	(
		/*HitResult*/ OutHit,
		/*out*/ bBlockingHit, 
		/*out*/ bInitialOverlap, 
		/*out*/ Time, 
		/*out*/ Distance, 
		/*out*/ Location, 
		/*out*/ ImpactPoint, 
		/*out*/ Normal, 
		/*out*/ ImpactNormal, 
		/*out*/ PhysMat, 
		/*out*/ HitActor, 
		/*out*/ HitComponent, 
		/*out*/ HitBoneName, 
		/*out*/ HitItem, 
		/*out*/ FaceIndex, 
		/*out*/ TraceStart, 
		/*out*/ TraceEnd
	);

FHitResult is a parameter for LineTraceSingleByChannel (passed by a reference FHitResult&). And after that, you just access member using . (For example, FHitResult MyHit; FVector MyLocation = MyHit.Location;).
Translate BP to C++ means understanding your BP code logic and ability to reproduce it’s logic in C++. I suggest you to find some C++ basics tutorial - it’s difficult to write C++ code without knowledge of syntaxis.

1 Like

Solved
I have learned to create and access my own collision channel.

1 Like

Yes it compiles and works perfectly, the actor spawned in the level dropped on the ground perfectly.

If You want I can share the whole logic including .h file.