TMap with Key/Value UserWidget pointers lose references on Compile

I have a UserWidget subclass with a TMap UPROPERTY where the key and value are both UUserWidget pointers (ie TMap). After creating a BP instance of my widget, the inspector lets me properly assign to the map without issue, showing the correct available widgets in the Hierarchy. If I save the Widget in the editor, those values persist. However, if I Compile the blueprint, I’ll lose the values every time, and all the Map entries will be empty.

Any ideas?

I’m afraid you can not use UUserWidgets as keys, you could try to generate a unique ID as your key and the value could contain a struct holding your pair (or even TPair).

To define your own keys what you normally do is to define how to compare your keys calsses and create a MapKeyFunction that the TMap will use to compare one to another.

The following is an example that you could follow:

#include "Engine/EngineTypes.h"

template<typename ValueType>
struct TMyClassMapKeyFuncs : TDefaultMapKeyFuncs<UMyClass, ValueType, false>
{
	static FORCEINLINE bool Matches(UMyClass A, UMyClass B)
	{
		return A == B; // Do your own compare here!
	}

	static FORCEINLINE uint32 GetKeyHash(UMyClass Key)
	{
		return ::GetTypeHash(Key);
	}
};


TMap<UMyClass, UUserWidget, FDefaultSetAllocator, TMyClassMapKeyFuncs<UUserWidget>> MyTMap;

The code above is written out of my head so it might need some love ^^

Cheers,
Moss