How to remove nullptr as a key from TMap
I am writing a Threat Map for target selection for battle that has a large number of AI. Sometimes, here is my Tmap declaration:
Like so, I am using the Character ref to find the threat value in the TMap. Sometime, the key = nullptr, and I try to remove it with ThreatMap.Remove(Key), however the Tmap nullptr key element is still there. Anyone know what is going on?
asked Mar 03 '18 at 01:39 AM in C++ Programming
First of all, it's a little odd that you're using a pointer as a key in an associative array. It's not wrong necessarily, just strange. I think personally I'd use the name of the character instead of the pointer to it.
Second, how are you using this? You're not putting nullptr into the TMap yourself, are you? (Don't.) It's that you have a character that has been removed from the scene, so its reference is null right? If that's the case, then it's likely because the garbage collector doesn't care about the TMap keys. (But that's just a guess, I haven't looked at the TMap code). To test that theory, Try making an additional TArray and put each character in that array as well. The garbage collecter does respect the TArray, so it will prevent the characters from being removed while they're still in use in the TMap.
Obviously that shouldn't be your final solution, but it might help you figure out what is happening.
Follow this question
Once you sign in you will be able to subscribe for any updates here