Casting an interface that comes from a HitResult from a SphereTraceMultiForObjects

Hello guys, I need some help on it, I did it a lot with blueprints, but now trying to do it in C++ I get stuck in an error.

I have an interface called ULockOnInterface, it is blueprint callable

// This class does not need to be modified.
UINTERFACE(MinimalAPI, Blueprintable)
class ULockOnInterface : public UInterface
{
	GENERATED_BODY()

};

/**
 * 
 */
class DMETPRISONPROJECT_API ILockOnInterface
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
		
};

I am making this hit with Sphere, it is working

bool hit = UKismetSystemLibrary::SphereTraceMultiForObjects(GetWorld(), startPoint, startPoint, 750.0f, ObjectTypesArray, false, ActorsToIgnore, EDrawDebugTrace::ForDuration, HitArray, true, FLinearColor::Gray, FLinearColor::Blue, 30.0f);

but when I try to cast the actor to my interface in this part of the code:

for (FHitResult hitResult : HitArray)
{

	AActor* hitActor = hitResult.GetActor();

	ULockOnInterface* myLock = Cast<ULockOnInterface>(hitActor);
	
	if (myLock)
	{
		LockOnTargets.AddUnique(*hitResult.GetActor());
		Distance = 1000000.0f;
	}
}

I get an error compiling:

1>D:\EPIc\Unreal 5.5\UnrealEngine\Engine\Source\Runtime\Core\Public\Containers\Array.h(1014): error C2678: ‘==’ binary : binary ‘==’ : no operator found which takes a left-hand operand of type ‘const AActor’

Someone can help me with it?

I tryed it too:

	const AActor* hitActor = hitResult.GetActor();

	const ULockOnInterface* myLock = Cast<const ULockOnInterface>(hitActor);

but same result.

Interfaces are a bit finicky in Unreal. When you want to access an interface in C++, you usually want to cast to the class that has an ‘I’ prefix. For example:

ILockOnInterface* myLock = Cast<ILockOnInterface>(hitActor);

For your example, since you only want to check if the interface is implemented, there is a function within an Actor class called Implements() or ImplementsInterface() I believe that will get you the same functionality. Here is some pseudo code

if (hitActor->Implements<ULockOnInterface>())

Thanks, it worked, but not is the error, the error is in other part, and I fixed the other part that is the error, sorry about that mate.

if (hitActor->Implements<ULockOnInterface>())
{			
	LockOnTargets.AddUnique(hitResult.GetActor());
	Distance = 1000000.0f;
}

the error was here, GetActor() returns a const AActor* and my TArray LockOnTargets was a TArray<Aactor*>, now I make it a const AActor* Tarray and works.

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