Check if object is of type TSubclassOf<>?

So I have three blueprint classes derived from UObject and I store them in TArray<UObject*> but then I want to find an object in the array that is from a
UPROPERTY(EditDefaultsOnly) TSubclassOf<UObject> MyBPClass
How can I do this?

Basically I wish I could either:

  1. if (Cast<MyBPClass>(Objects[0]))
  2. if (MyBPClass == Objects[0]->Class)

using == checks for an exact match, not if a class is a subclass. Casting does check if a class is an exact match or one of its parent base classes.

if (!IsValid(MyBPClass)) {
  return;
}

for (UObject* ObjectX : Objects) {
  if (ObjectX->GetClass() == MyBPClass->GetClass()) {
    // DoStuff
  }
// or
  if (ObjectX->IsA(MyBPClass->GetClass())) {
    // DoStuff
  }
}

This does not work:

MyBPClass* MyBPObject = Cast<MyBPClass>(ObjectX))
3 Likes

Hmm Are you saying that MyBPClass is a TSubclassOf?
if so this is still not working for me!
Here is my exact code:

// InactiveAttackPool: TArray<UAttack*>  (UAttack inherits from UObject)
// AttackClass: TSubclassOf<UAttack>
if (InactiveAttackPool[i]->GetClass() == AttackClass->GetClass())
{ 
// do stuff

== does an exact match check. Can you test the IsA method?

1 Like

If you’re doing GetClass() already just do GetClass()->IsChildOf(OtherThing), else use IsA(OtherThing)

1 Like

So:
InactiveAttackPool[i]->IsA(AttackClass) β†’ true
but
InactiveAttackPool[i]->GetClass() == AttackClass->GetClass() β†’ false

So I’m going to use IsA()
Thanks

Is comparable with:
5 β†’ is a number? β†’ true
5 == 3 β†’ false
As in an apple is a fruit but they are not the exact same thing.