I can’t find a simple way to do this using casting so I’m opening this to the floor… Essentially I have a base ‘Weapon’ class, which has numerous sub-classes created as Blueprints in the editor.
What I want to do, is check whether two different instances of a class are the same. I also do NOT want it to return true if the second class is a child of the first, so they need to be absolutely identical to return true.
That’s what I tried first but unfortunately it doesn’t work, since the check returns true if they are the same C++ class, regardless of the Blueprint type.
What I’ve done for now is just compare the name of the weapons, since I think they will be specific to each weapon blueprint the majority of the time.
Are you looking for full value-type semantics equality comparison or do you mean just type-equality comparison?
If looking for value-type semantics, I think I do recall seeing a macro that forces default equality operators to not be auto-generated for UCLASS types, but I’m not 100% sure and it would create a fair bit of extra work. What you could do is implement an interface that provides a common basis for equality comparison suited to your needs.
If you’re looking for type-comparison you could implement some sort of integral value that works as a key/id making identification simple, although this may not be practical for your needs. Off the top of my head, you could even loop through GetOutter() on each instance, comparing each iteration for equality.
Normally I would suggest hash value comparison for either scenario when stumped but in unreal, object hashes aren’t quite unique enough for hashes to be reliable.
Basically because both objects I was doing ‘GetClass()’ on are sub-classes of ‘UBZGame_Weapon’, the check to see if they are equal was returning true (at least, that’s how I imagine it was working). Although they were different subclasses and different blueprints. What I really wanted to do was check that the Blueprint was the same, not the class they derive from.
I’ve managed to get around this for now by checking the name of the weapon (just an FName variable stored in each sub-class) against the other ones. So long as the names I give to each are different, then it won’t have any impact. It’s (possibly) quicker than comparing the two classes as well am I right?
Comparing the results of GetClass is just a pointer comparison, you can’t get quicker.
It should work as you intend and differentiate exact classes/blueprints. You’d need to be calling the method on actual instances though, not on a UClass object. It would be clearer if you could post a code snippet of the situation you want to make the test, and what you tried.
The proper method to check if two classes are exact is by comparing their UClass.
UClass* ThisClass = GetClass();
UClass* OtherClass = AWeapon::StaticClass();
if (ThisClass == OtherClass)
{
// Stuff here will only be called for the base AWeapon class, and non of its subclasses.
}