Download

How to access TSubclassOF<> via property iterator?

I have struct property iterator and I need to handle TSubclassOf<> property.

I have a structure with a TSubclassOf<> variable named “ItemClass”.
I managed to get FClassProperty, but I can’t find a way to get the TSubclassOf variable out of it.



void UItemMaster::HandleStruct(FProperty* Property, void* StructPtr)
{
    int64 ItemHash = 0;
    TSubclassOf<UItemBase> ItemClass;
    
    FStructProperty* StructProperty = CastField<FStructProperty>(Property);
    if (StructProperty)
    {
     TFieldIterator<FProperty> PropertyIterator(StructProperty->Struct);
    
        for (PropertyIterator; PropertyIterator; ++PropertyIterator)
        {
            for (int32 ArrayIndex = 0; ArrayIndex < PropertyIterator->ArrayDim; ArrayIndex++)
            {
                void* ValuePtr = PropertyIterator->ContainerPtrToValuePtr<void>(StructPtr, ArrayIndex);

                if (PropertyIterator->GetAuthoredName() == TEXT("ItemClass"))
                {
                    if (FClassProperty* ClassProperty = CastField<FClassProperty>(*PropertyIterator))
                    {
                        GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, *ClassProperty->GetName());
                        // here i need to convert ClassProperty to TSubclassOf<>
                    }
                }
            }
        }
    }
}


I searched on the Internet, but found nothing useful.

I think I found an answer, but it uses FName of class. I don’t shure is this safe or not.



               ItemClass = ClassProperty->GetPropertyValue(ValuePtr);
                        
                        if(UItemBase* NewItem = NewObject<UItemBase>(this, ItemClass->GetFName()))
                        {
                            GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, NewItem->ItemName.ToString());
                        }