How are we supposed to keep a custom TSet alive? By custom, I mean TSet that has a user-specified KeyFuncs.
UPROPERTY() only supports default TSet, and it seems that AddReferencedObjects too.
As a result, my TSet are destroyed as soon as they stop containing a referenced object.
Here’s a short example.
- A class UMyIndexedObject that will be used as a key for the TSet
UCLASS(Blueprintable)
class UMyIndexedObject : public UObject
{
GENERATED_BODY()
private:
UPROPERTY()
FString MyIndex; // the index key
public:
FString GetIndex() const { return MyIndex; }
//...
}
- A custom KeyFuncs for that class
struct FMyKeyFuncs : BaseKeyFuncs<UMyIndexedObject*, FString>
{
typedef FString KeyInitType;
static KeyInitType GetSetKey(const UMyIndexedObject* Obj) { return Obj->GetIndex(); }
static bool Matches(KeyInitType A, KeyInitType B) { return A == B; }
static uint32 GetKeyHash(KeyInitType Key) { return GetTypeHash(Key); }
};
- And a random class to declare and use the TSet:
UCLASS()
class USomeClass : public UObject
{
GENERATED_BODY()
protected:
UPROPERTY() // <--- fails here
TSet<UMyIndexedObject*, FMyKeyFuncs, FDefaultAllocator> UnstableSet;
//...
};
Problems here:
- I can’t compile with UPROPERTY.
- Compile error message is wrong (it says I cannot use a non-default Allocator, while I’m actually using a non-default KeyFuncs.
- Non overload function found when trying to use AddReferencedObjects (seems to only support default TSet, like UPROPERTY)
So how are we supposed to use this nice custom KeyFuncs feature of TMap/TSet without loosing the whole Set to the garbage collector?