I have a function that is meant to create and return a new TMap. However, I’m not sure how to dynamically create a new TMap. I tried NewObject<TMap<FString, FString>>(), but that gives me a compile error.
I can do it using “new”, but I understand that this will make it not work with Garbage Collection.
What is the proper way to do this.
Thanks
TMap isnt a UObject,
just create a variable
TMap<FString, FString> MapName;
Thanks. The problem is that I need to create a TMap inside a function and return a pointer to it. If I just create is as a stack variable, it’ll get destroyed when the function exits and this gives me a compiler error to the effect that I can’t return a local variable pointer. I really need to create it on the heap to ensure it persists beyond the function lifetime, so normally, I would need to use “new TMap<…>”. But does that mean that I have to take care of destroying it manually later, or does TMap have some built in GC behavior. Alternately, should I put it inside a TSharedPtr instead, which will destroy it when there are no more references to it?
I have something like this that seems to work. Hopefully it can help you.
Header:
static TMap<ECharacterPose, float> AddMap(TMap<ECharacterPose, float> Sums,
const TMap<ECharacterPose, float>& Addends);
CPP:
TMap<ECharacterPose, float> AGameModeArsenal::AddMap(TMap<ECharacterPose, float> Sums,
const TMap<ECharacterPose, float>& Addends)
{
for (const auto& Addend : Addends)
{
if (Sums.Find(Addend.Key))
{
Sums[Addend.Key] += Addend.Value;
continue;
}
Sums.Emplace(Addend.Key, Addend.Value);
}
return Sums;
}
Called from a void non-static method:
Multi->AdsSpeeds = AddMap(Multi->AdsSpeeds, Raw->AdsSpeeds);