I’m not sure if you can cast on TSubclassOf, as class is not an object.
I think you would need first to spawn actor from that class and then cast it to whatever.
You can’t cast classes to other classes. Class is just “blueprint” for object that will be created. You can cast objects to other objects that are in the same hierarchy. Could you explain what exactly are you trying to achieve ?
Do you want “Item” to represent an instance of some item (an actual item object that has already been created)? Or do you want it to be a type of object (to maybe spawn at a later time or something)?
If you want it to point to an actual item, then you probably want to use TSubobjectPtr instead of TSubclassOf, like so:
If instead you did want “Item” to be a type (an item class), you can check if that class is a AShooterWeapon or not with this:
Item->IsChildOf(AShooterWeapon::StaticClass());
Lastly, the “class” prefix before “SomeClass” is a C++ syntax feature. You are forward declaring that class. The header file that you are in doesn’t know what “SomeClass” is. Because the file doesn’t use the nitty-gritty insides of SomeClass, you can just “forward declare” it and let the file know just what type it is. For more information, here’s a wikipedia article: Forward declaration - Wikipedia
That’s because Item is a class and not an instance of AShooterWeapon. See my answer below for full details, but I think you want to use TSubobjectPtr instead of TSubclassOf.
This function is used to apply persistent effects to actors (like damage over time etc). Effects are implemented as Blueprints. I assumed that each effect can have unique gameplay mechanics, and didn’t wanted to code everything in C++.
I of course could pass effect as object to function, but I found it wasn’t really that clean and could generate confusion, for someone who doesn’t know, why object should be created before passing.
Got ya! Yeah, it looks like you do want TSubclassOf (sorry I didn’t fully understand what you were trying to do before). Here, let me suggest trying this:
if (Item->IsChildOf(AShooterWeapon::StaticClass()))
{
TSubclassOf<AShooterWeapon> WeaponClass = *Item;
CanPickupAmmo(WeaponClass);
}
This was something I came across as well, I wasted to cast a reference pointer for a class
TSubclassOf<ACustomObjectBase> TemplateObjectBase;
const TSubclassOf<AActor> L_TemplateObjectBaseTSub{ TemplateObjectBase->GetClass() };
// or if I want to cast back to a pointer
const AActor* L_TemplateObjectBasePtr{ Cast<AActor>(TemplateObjectBase) };