Is there any info on why this issue is marked as “Won’t fix”? I have this issue in C++ as well, trying to achieve the same as @Spoondog .
CollectionUtils.h
/**
* Zips together two input arrays to create a map. Works similar to pythons "Zip" function, but:
* - Only indices existing in both arrays are inputted into the map.
* - Equal entries of `Keys` result in one map entry with the last value being present in the map
*
* @param Keys The input array to create keys from
* @param Values The input array to create values from
* @param TargetMap The map to fill from the two arrays
*/
UFUNCTION(BlueprintCallable, CustomThunk, meta=(DisplayName = "Zip", CompactNodeTitle = "ZIP", MapParam = "TargetMap", MapKeyParam = "Keys", MapValueParam = "Values", AutoCreateRefTerm = "Keys,Values"), Category = "Collection Utils")
static void ZipToMap(const TArray<int32>& Keys, const TArray<int32>& Values, TMap<int32, int32>& TargetMap);
DECLARE_FUNCTION(execZipToMap)
{
// `Keys` Array
Stack.MostRecentProperty = nullptr;
Stack.StepCompiledIn<FArrayProperty>(NULL);
void* KeysAddr = Stack.MostRecentPropertyAddress;
FArrayProperty* KeysProperty = CastField<FArrayProperty>(Stack.MostRecentProperty);
if (!KeysProperty)
{
Stack.bArrayContextFailed = true;
return;
}
// `Values` Array
Stack.MostRecentProperty = nullptr;
Stack.StepCompiledIn<FArrayProperty>(NULL);
void* ValuesAddr = Stack.MostRecentPropertyAddress;
FArrayProperty* ValuesProperty = CastField<FArrayProperty>(Stack.MostRecentProperty);
if (!ValuesProperty)
{
Stack.bArrayContextFailed = true;
return;
}
// `TargetMap` Map
Stack.MostRecentProperty = nullptr;
Stack.StepCompiledIn<FMapProperty>(NULL);
void* TargetMapAddr = Stack.MostRecentPropertyAddress;
FMapProperty* TargetMapProperty = CastField<FMapProperty>(Stack.MostRecentProperty);
if (!TargetMapProperty)
{
Stack.bArrayContextFailed = true;
return;
}
P_FINISH;
P_NATIVE_BEGIN;
GenericZipToMap(KeysAddr, KeysProperty, ValuesAddr, ValuesProperty, TargetMapAddr, TargetMapProperty);
P_NATIVE_END
}
static void GenericZipToMap(
const void* KeysAddr, const FArrayProperty* KeysProperty,
const void* ValuesAddr, const FArrayProperty* ValuesProperty,
const void* TargetMapAddr, const FMapProperty* TargetMapProperty);
CollectionUtils.cpp
void UCollectionUtils::GenericZipToMap(
const void* KeysAddr, const FArrayProperty* KeysProperty,
const void* ValuesAddr, const FArrayProperty* ValuesProperty,
const void* TargetMapAddr, const FMapProperty* TargetMapProperty)
{
if(KeysAddr && ValuesAddr && TargetMapAddr &&
ensure(TargetMapProperty->KeyProp->GetID() == KeysProperty->Inner->GetID()) &&
ensure(TargetMapProperty->ValueProp->GetID() == ValuesProperty->Inner->GetID()))
{
FScriptMapHelper TargetMapHelper(TargetMapProperty, TargetMapAddr);
FScriptArrayHelper KeysArrayHelper(KeysProperty, KeysAddr);
FScriptArrayHelper ValuesArrayHelper(ValuesProperty, ValuesAddr);
KeysArrayHelper.EmptyValues();
ValuesArrayHelper.EmptyValues();
FProperty* KeysInnerProp = KeysProperty->Inner;
FProperty* ValuesInnerProp = ValuesProperty->Inner;
int32 Size = KeysArrayHelper.Num();
for( int32 I = 0; Size; ++I )
{
if(KeysArrayHelper.IsValidIndex(I) && ValuesArrayHelper.IsValidIndex(I))
{
uint8* KeyElementPtr = KeysArrayHelper.GetRawPtr(I);
uint8* ValueElementPtr = KeysArrayHelper.GetRawPtr(I);
TargetMapHelper.AddPair(KeyElementPtr, ValueElementPtr);
}
}
}
}
When using the function in Blueprint, I even get the correct color coding (more than @Spoondog showed in screenshots) and I can connect the arrays. But on compilation, I get errors that suggest that the value array is not interpreted correctly: