Ignore self using SweepSingleByChannel in C++

Hi,

I’m trying to hard-code my Blueprint functions in order to improve the performance.

Unfortunately, when i try to pass the nex function to C++, it get that the character is damaged for its attack, when the Blueprint function does not.

Looking carefully in te function i get that SphereTraceSingle have a bool option to ignores the “actor” itself, but i don’t get how can i do the same thing in C++ using SweepSingleByChannel.

How can i solve it?

The first thing that we can do to understand what is happening is going to the Blueprint’s function definition:
a short searching in Google(SphereTracebySingle UE4 C++) sends us to the documentation, there we can see where is the folder when we can find the respective file in the code: Engine\Source\Runtime\Engine\Classes\Kismet\KismetSystemLibrary.h. Here, we can see that the code name of this function is SphereTraceSingle and if we go to its declaration we will fine its parameters and its code:

bool UKismetSystemLibrary::SphereTraceSingle(UObject* WorldContextObject, const FVector Start, const FVector End, float Radius, ETraceTypeQuery TraceChannel, bool bTraceComplex, const TArray<AActor*>& ActorsToIgnore, EDrawDebugTrace::Type DrawDebugType, FHitResult& OutHit, bool bIgnoreSelf, FLinearColor TraceColor, FLinearColor TraceHitColor, float DrawTime)
{
	ECollisionChannel CollisionChannel = UEngineTypes::ConvertToCollisionChannel(TraceChannel);

	static const FName SphereTraceSingleName(TEXT("SphereTraceSingle"));
	FCollisionQueryParams Params = ConfigureCollisionParams(SphereTraceSingleName, bTraceComplex, ActorsToIgnore, bIgnoreSelf, WorldContextObject);

	UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
	bool const bHit = World ? World->SweepSingleByChannel(OutHit, Start, End, FQuat::Identity, CollisionChannel, FCollisionShape::MakeSphere(Radius), Params) : false;

#if ENABLE_DRAW_DEBUG
	DrawDebugSphereTraceSingle(World, Start, End, Radius, DrawDebugType, bHit, OutHit, TraceColor, TraceHitColor, DrawTime);
#endif

	return bHit;
}

From this code what is interesting for us is bool bIgnoreSelf and we can see this parameter is used in the Params variable when we use the ConfigureCollisionParams to create is as a FCollisionQueryParams; we use this parameters to control the trace.

We want to know what ConfigureCollisionParams doest, so we go to its declaration and we see it returns a FCollisionQueryParams variable with its configuration adding a AActor to the Actors’s ignored list; so, we get we need add the our actor to the paramet’s ignored list as we can find in the FCollisionQueryParams's documentation:

FCollisionQueryParams Params(FName(TEXT("KnockTraceSingle")), true,  Actor);
Params.bReturnPhysicalMaterial = true;
Params.bReturnFaceIndex = !UPhysicsSettings::Get()->bSuppressFaceRemapTable;

As our function pass a AActor as the instigator, we want also this actor to be ignored, as this actor is own character, so, finally we add this parameter to the SweepSingleByChannel function:

bool bIsHit = GetWorld()->SweepSingleByChannel(OutHit, SocketLocation, EndTrace, FQuat::Identity, ECC_WorldStatic, FCollisionShape::MakeSphere(LengthTraceValue), Params);

And it’s all.