Announcement

Collapse
No announcement yet.

How to access TSubclassOF<> via property iterator?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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.

    Code:
    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.
    Last edited by Nick Tsaizer; 10-24-2020, 04:56 AM.

    #2
    I think I found an answer, but it uses FName of class. I don't shure is this safe or not.
    Code:
                   ItemClass = ClassProperty->GetPropertyValue(ValuePtr);
                            
                            if(UItemBase* NewItem = NewObject<UItemBase>(this, ItemClass->GetFName()))
                            {
                                GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, NewItem->ItemName.ToString());
                            }

    Comment

    Working...
    X