ACharacter to AActor comparison error

Why this doesn’t work:

if (OtherActor == UGameplayStatics::GetPlayerCharacter(this, 0)) {
    // something
}

ACharacter is an AActor subclass, so i don’t get the error.

The error I get when compile is:

CompilerResultsLog: Error: D:\Unreal Engine\Cookbook\Source\Cookbook\Pickup.cpp(49) : error C2446: ==': no hay conversi?n de 'ACharacter *' en 'AActor *'
CompilerResultsLog: Error: D:\Unreal Engine\Cookbook\Source\Cookbook\Pickup.cpp(49) : note: Los tipos se?alados no est?n relacionados; la conversi?n requiere reinterpret_cast, conversi?n de estilo de C o conversi?n de estilo de funci?n
CompilerResultsLog: ERROR: UBT ERROR: Failed to produce item: D:\Unreal Engine\Cookbook\Binaries\Win64\UE4Editor-Cookbook-5290.dll

(I’m sorry, my compiler it’s in spanish).

Thanks in advance.

Your compiler is pretty much telling you the issue. You can’t compare two pointers that are not of the same type.

I’m guessing that bit of code comes from an overlapping event handling function. If you want to check that it is in fact the player that is touching the object, there are other ways.
You could use tags for instance. But I wouldn’t compare pointers like that.

Thanks, I didn’t know about tags. I added a tag to de FirstPersonCharacter:

210319-captura.png

And change the code like this:

void APickup::OnPickedUp(UPrimitiveComponent * OverlappedComponent, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult) {
	if (OtherActor->Tags.Max() > 0) {
		UE_LOG(LogTemp, Warning, TEXT("Collision with %s"), *OtherActor->Tags[0].ToString());
		if (OtherActor->Tags[0].ToString().Equals(TEXT("Player"))) {
			Destroy();
		}
	} else {
		UE_LOG(LogTemp, Warning, TEXT("Collision"));
	}
}

But, why the comparison doesn’t work if AActor is a parent of ACharacter?

Quick answer :

C++ pointer equality doesn’t work that way.

Documented answer :

If you’re really curious, you can read the C++ 11 specifications, chapter 5.10 Equality operators.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf

Also, another note :
AActor class has a function for checking tags, ActorHasTag(), so no need to do it manually reading from the Tags array.

But this is not your problem. Your pointers are compatibles, so your code is OK.

The error error C2446: ==': no hay conversi?n de ‘ACharacter *’ en ‘AActor *’ and others of the same type are frequent since 4.15.

You need to add
#include “GameFramework/Character.h”
to your Pickup.ccp list of #include.

The reason: ACharacter is now simply referenced by class ACharacter; a forward declaration, the compiler cannot know it’s an AActor derived class until you declare the “real” class (with the proper #include)

This should be the accepted answer. It kills me how often I fall for missing imports and the error indicating nothing of the sort. Been down this rabbit hole so many times and it got me again.