Maybe it could be like that:
FHitResult ATraces::Trace(FVector start, FVector end)
{
FHitResult hitResult;
FCollisionQueryParams CombatCollisionQuery(FName(TEXT("CombatTrace")), true, NULL);
// You can also use "nullptr" instead of "NULL", they both equals zero by the way
CombatCollisionQuery.bTraceComplex = true;
CombatCollisionQuery.bTraceAsyncScene = false; // this
CombatCollisionQuery.bReturnPhysicalMaterial = false; //and this by default are false, but it is ok forcing them to false, because if you will read this code again, you will be sure, that this trace isn't async etc.
actor.LineTraceSingleByChannel (hitResult, start, end, ECC_WorldStatic, CombatCollisionQuery);
return hitResult;
}
void AMyCharacter::Cast()
{
//This code may be much cheaper
FHitResult hitResult = t->GetDefaultObject<ATraces>()->Trace(GetActorLocation(), GetActorLocation() + FVector(0.0f, 0.0f, -100.0f));
//And something down there...
And if you want have single Actor in scene, which will only do tracing, so you can make one magic trick - static variable.
In .h of an ATrace somewhere in public field write something like this:
static ATraces * globalPointer;
In .cpp you need to declare it, set it’s default value:
ATraces * ATraces::globalPointer = nullptr;
Also in .cpp, but in initializator method:
globalPointer = this;
Very fast and pretty safe, only if your actor will start with the scene, otherwise sometimes this variable equals nullptr, until first ATraces created in scene. If your ATraces already in level, then you don’t need to worry about this ![]()
And now from every place you can use ATraces::globalPointer->DoSomething(), if you have #include “Traces.h” of course.