How to compare an actor with a character?

Hello,

The title is pretty much self explanatory. I want to compare an actor, called OtherActor, with a character, called APlayerCharacter, in an if-statement. The function I’m using is an BeginOverlap event used for boxcollision, and I’m trying to simply check if OtherActor, or the actor that has entered/left the boxcollision, is a character of class APlayerCharacter, which is my player character class derived from character.

This is the only code I’ve tried that doesn’t compile with any errors:

if (OtherActor->StaticClass() == APlayerCharacter::StaticClass())
{
    // Code here
}

Fair enough, this doesn’t give me any errors, but it doesn’t seem to recognize my character whenever I enter or leave the box. I’ve tried using only GetClass() instead of StaticClass() but it just returns red underlines below “APlayerCharacter::GetClass()”.

Does anyone know the syntax for this? How do I do it, do I use GetClass() or StaticClass() and how?
Thanks in advance for any help! :slight_smile:

~Stefan

I’m trying to simply check if OtherActor, or the actor that has entered/left the boxcollision, is a character of class APlayerCharacter, which is my player character class derived from character.

No problem. If you’re trying to determine whether the actor that has triggered the overlap is derived from a certain class, why not try casting to that class type and see if the cast was successful?

APlayerController* MyPlayerControllerActor = Cast<APlayerController>(OtherActor);
if (MyPlayerControllerActor != nullptr)
{
     // Cast was successful; OtherActor is an instance of a 
     //  class that derives from APlayerController!  Do stuff!
}

Otherwise, if you have a pointer to the APlayerController instance that you want to compare against (let’s call that pointer MyPlayerActor for this example), then there’s no need to perform casting or to involve comparisons of class types. Just compare the two pointers directly:

if (OtherActor == MyPlayerActor)
{
     // OtherActor is the player!  Do stuff!
}

As long as you aren’t dealing with pointers to pointers (which are uncommon, and not something you’re likely to create by accident), it doesn’t have to get any more complicated than that!

Hope this helps!

I actually tried to cast to APlayerCharacter directly in my if-statement, but it didn’t work. Although your code works perfectly fine! If anyone is interested here’s my code, although it pretty much is the same as the above:

	APlayerCharacter* DetectedPlayer = Cast<APlayerCharacter>(OtherActor);

	if (OtherActor == DetectedPlayer)
	{
		// Do stuff
	}

Thank you so much for your help! I really appreciate and love this unreal engine community, so many experienced and active members that will help you with anything. Thanks! :slight_smile: