UE4 C++ Compare Overlap Actor Class

Hello everyone, I am currently trying to get the “other” actor that is overlapping “this” actor to check and compare whether the “other” actor have a similar class as the one I wanted to compare it with.

The image below is what I would like to try to compare the “other” actor with the same class.

The image below is something which is similar in comparison when used as a blueprint, it didn’t need to compare before casting as it seems to already check for the correct class.

266551-overlapcheck0.png

One of the problems I wanted to try and solve regarding this is that it seems to cast the “other” actor into an invalid class no matter which actor is overlapping it (which could be causing a crash), so I thought adding a condition before the actual casting may solve this problem.

It should try and compare the overlapping actor with the correct class to function properly.

Any help regarding this is appreciated.

It seems that I still can’t figure out the code to do this correctly, any thoughts?

Classes in C++ (since this is normal C++) can not be used as variable type as there no class information on run time, class information is only avable on compile time.

You should able to figure out what to use yourself, and simple question what type GetClass function return to be able to compare it with anything.

And it is UClass pointer, UClass is a object which represents the class it self in reflection system.

As i said before class infomation is lost after compilation, not only tha,t all names, function names, varbales names varbale types and so on are also lost. There no way to retive that information, as it is only used during compilation to construct CPU machine code and all variables are are turned in to memory address offsets and they embedded in to instructions. Only way to track that is to program application that keep that information, so program can see how it is structured, see reflection of it self, “reflection system” ;] usually this require calling function which register classes, but UE4 provides tool that generate that registration code for you called UnrealHeaderTool (UHT), which captures all those UCLASS() UPROPERTY() macros (that does nothing in compilation they only used by UHT), and generated code generate varius objects representing entities of the code, which you can use in runtime

So each class when your code module is loaded is being registered by generated code and each class have it own UClass object contianing information about class, which you can get using GetClass() function. Not only that but all entires that UHT detect has there onw object like that structures have UStruct, functions have UFunction, properties have UProperty(), enums got UEmum and so. All of those object inherent from UField and oyu can explore it in API refrence:

Now back to begining, you can not use class as a variable in C++ you need to get UClass object without a object of a class and to do that you need to use StaticClass() function (which is generated by UHT too) which is static function returning UClass of used class namespace, every UObject class have it, so you should do it like this:

if(OtherActor->GetClass() ==APacmanPawn::StaticClass()) {

}

There one issue with this, this will return true only if and just if OtherActor is that exact class (APacmanPawn), but if you would create other C++ class or blueprint based of that class, both would be different class it would nto be the same as APacmanPawn, have there own UClass, there for that statement would return false. There special function in UObject called IsA, which not only checks if class is of perticilar class but also checks if class of object is related to that based class which allows casting:

So you can do this:

if(OtherActor->IsA(APacmanPawn::StaticClass())) {
   
}

And it will be true if class of actor is just related to APacmanPawn and oyu can cast to it ^^

Now as extra, you said that blueprint can just cast to check the type… you can do exact same thing in C++. UE4 provides special casting function which automatically checks the the type of object which you attempt to class and return castef object if it is… if it’s not it return null pointer and you know it’s not. That function is Cast<>() and it’s template function which you can input class (template is kind of like macros which let you kind of paste code, but instead compiler creates version of function or class for each template argument combination used, you can learn about them here: /doc/oldtutorial/templates/). So you can do like this:

APacmanPawn* Pacman = Cast<APacmanPawn>(OtherActor);
     
if(Pacman) {
       
}

So we create local varable, Pacman of type APacmanPawn* we use Cast<>() to cast OtherActor to APacmanPawn*, now since we know that Cast<>() return nullptr if class that does not match, we can just check if Pacman is not null pointer to know if it’s class we looking for. Similar way as Cast node works, but thAT node automatically do if statement (Branch) for you.