Accessing objects of TSubclassOf

Hi Guys,

I have a property on my character for apparel, this item is set in blueprints so the designer can set the item to adorn the character with. The code for this in the header file is:

UPROPERTY(EditAnywhere, Category = “Character Accessories”)
TSubclassOf AccessoryHat;

I then instantiate the item like this:

AIGCAccessory* HatAccessory = ()->SpawnActor(AccessoryHat, FVector::ZeroVector, FRotator::ZeroRotator, Spawnparams);

if (HatAccessory)
HatAccessory->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, HatSocketName);

My question is how do i access the AccessoryHat blueprint to run functions on it, for example detaching it from the character. In the above code example if i expose the HatAccessory value i can run functions on it, but is there a way to do this directly on the TSubclassOf AccessoryHat variable.


No, TSubclassOf is template type of UClass* so you in reality talking to UClass object thru this variable. You need to have instantiated object somehow to do function call on it, UClass just a identifier not a actor.

If you thinking of defaults, having class varbales there and then spawn on begin play is not a bad practice. All you need to do is create:

AIGCAccessory* HatAccessory;

inside the class to keep access to spawned accessory.

Other for you would be creating components (by extending UChildActorComponent? or even UStaticMeshComponent) out of accessories so you can just place them in blueprint , but you won’t escape having slot variables. Those components would need to say to owner actor “oh hi, im here” so they can be placed on proper slot.

Thanks , i’m stilling getting my head around the ways to access the objects, but that works.