How compare Actor of a certain component to the player Actor

So I have a component attached to multiple actors. Some which are AIs. I want to separate some functionality using a simple if statement. Now I know I could separate this using subclasses but that will not do what I want. I specifically want this “dirty” way of doing it (just debugging).

From the component I can get the owner like so:

AActor *b = Cast<AActor>(this->GetOwner());

But trying to get the first player controller actor crashes the editor.

AActor *a = GetWorld()->GetFirstPlayerController()->GetCharacter()->GetParentActor();

Even if that would not crash I cannot find how to properly compare two Actors to make sure it is in fact the correct one (in this case the possesed default player). Searching for answers I find some information but only in Blueprint and looking at the C++ API I cannot find the same functions.

Can anyone help me with this?

GetCharacter() returns an ACharacter object, and because ACharacter is a child class of AActor, the result is already the actor you want to retrieve. So there’s no need to call GetParentActor() on that, and this call will probably return NULL.

Thanks. I also noticed that the == operator works for Actor comparison.

Can you please give more details: I have this code to check collision, and it can NOT compare Actor with Character thou. How do I fix this?

in .cpp file:

void APickup::OnPickedup(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	
	if (OtherActor == UGameplayStatics::GetPlayerCharacter(this, 0)) {
		UE_LOG(LogTemp, Warning, TEXT(" ---- collision ---- "));
		Destroy();
	}
}

in .h file

UFUNCTION()
void OnPickedup(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

Cast otheractor to ACharacter and first check if this object is valid. then compare the casted acharacter with your playercharacter

… Many thanks, it works now… : ) ,

void APickup::OnPickedup(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	ACharacter* OtherCharacter = Cast<ACharacter> (OtherActor);
	if (OtherCharacter != nullptr) {
		if (OtherCharacter == UGameplayStatics::GetPlayerCharacter(this, 0)) {
			UE_LOG(LogTemp, Warning, TEXT(" ---- collision ---- "));
			Destroy();
		}
	}
}

@emperor_katax that snipit wasn’t working for me. This solution did the trick:

OtherActor == UGameplayStatics::GetPlayerPawn(this, 0)

many thanks dude … ; ] ,