Checking whether a certain unique value has been assigned to a struct member variable

I have a struct which contains several FName member variables, and a set of names, some of which are assigned to the aforementioned variables. Assuming that the number of member variables is large, given a name from the set, what is the best way to check if the name has been assigned to one of the member variables of the struct? I am guessing it could be done by using unreal type traits in a smart way but don’t know much about them to see how.

// Struct must be marked as USTRUCT, and all (relevant) properties must be marked as UPROPERTY, in order to be detected by the reflection system (RTTI)
USTRUCT()
struct FMyStruct
{
    UPROPERTY()
    FName Name1;
    //... ... ...
    UPROPERTY()
    FName Name99;
};

// can be UFUNCTION, static
bool MyStructContainsName(const FMyStruct& InStruct, const FName& InName)
{
    // Get RTTI for struct
    UScriptStruct* StructInfo = FMyStruct::StaticStruct();

    // Iterate all FName properties within StructInfo
    for (TPropertyIterator<FNameProperty> It(StructInfo); It; ++It)
    {
        const FNameProperty* Prop = *It;

        // Pointer to the value of this property
        const FName* ValuePtr = Prop->ContainerPtrToValuePtr<FName>(&InStruct);

        if (InName == *ValuePtr)
        {
            UE_LOG(LogTemp, Log, TEXT("Property '%s' contains value '%s'"), *Prop->GetNameCPP(), *(ValuePtr->ToString()));
            return true;
        }
    }
    return false;
}

Thank you, was able to get it done

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.