How to check AActor against ACharacter in C++?

Hi , I want to check if the OtherActor is my Current Character in Overlap function , how to do it?

What I am doing is:

void AMyTestCharacter::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	if (OtherActor == MyCharacter_Ref)
	{
		LogScreen("OverLapped");
	}
    return;
}

In Blueprints its working fine, but in C++ I have an error of incompatible types actor and character.

maybe because of this?

if (OtherActor == Cast<AActor>(MyCharacter_Ref))
1 Like

Have you included the headers?

#include "GameFramework/Character.h"
#include "GameFramework/Actor.h"

	AActor* A = .......;
	ACharacter* B = ...........;


Now I have this error.

yes, all the headers are included

what type is the character_ref? can you post the code? Looks like it doesn’t understand what type character_ref is. which is usually missing header, else it would be odd.

//Defining the refference to the BaseCharacter

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Refference Related)
AMyBaseCharacter * MyCharacter_Ref;

And is the header included of the file where that uproperty lives, in the cpp file where you make the actor comparison?

I dont know the exact syntax problem you have right now But I would tag the character reference with something like ‘ignoreThisOne’ and instead of comparing if A = B I’d check if A has tag ‘xxx’

1 Like

Headers are included in both, .h and .cpp

Character should not be ignored, the box collision collides with character by checking if the other actor is character … just print something… in blueprint the same logic has no problem comparing with character/actor but in C++ I don’t know how to check .

This compiles:

#include "GameFramework/Character.h"
#include "GameFramework/Actor.h"


void ASomeClass::YourMethod() {
	AActor* A = nullptr;
	ACharacter* B = nullptr;

	if (A == B) {
            LogScreen("OverLapped");
	}
}

if your custom character is in another .h than “GameFramework/Character.h” you include it

#include "GameFramework/Actor.h"
#include "YourCharacter.h"


void ASomeClass::YourMethod() {
	AActor* A = nullptr;
	AYourCharacter* B = nullptr;

	if (A == B) {
            LogScreen("OverLapped");
	}
}

Nothing else comes to mind.

1 Like

Restarted Visual Studio and Disabled Visual Assist, everything works fine… re-enabled Visual Assist and still everything works fine… don’t have any idea what it was :yawning_face:

2 Likes