How to dynamically define array inner type?

I want a function that adds a default element to any sort of TArray.

This is my code:

bool UMyBPFunctions::ArrayByName_AddElement(UObject* Target, FName Name)
{
        FProperty* property = Target->GetClass()->FindPropertyByName(Name);
        FArrayProperty* Array = Cast<FArrayProperty>(property);
        TArray<ArrayInnerClass>* outArray = Array->ContainerPtrToValuePtr<TArray<ArrayInnerClass>>(Target);
        outArray->AddDefaulted();
        return true;
}

The “ArrayInnerClass” part is pseudo-code. Thats the dynamic array-type/the part i dont know how to do.
Does anyone know how/if it is possible to do this?

2 Likes

There is a structure FScriptArrayHelper that you have to use for stuff like this and it would look something like (this is untested/compiled):

FProperty* property = Target->GetClass()->FindPropertyByName(Name);
FArrayProperty* Array = Cast<FArrayProperty>(property);

FScriptArrayHelper ArrayHelper( Array, Array->ContainerPtrToValuePtr< void >( Target) );
ArrayHelper.AddValue();

There’s a bunch more things you can do with it though if you check out the header with the structure’s definition.

ah perfect, thanks very much.