Class pointers as keys in dictionaries... Good or bad idea?

Months ago I started programming a very important component of my game.

At first I did it like this
TMap< TSubclassOf<ASomeBase>, int32> MyDic;

And everything was working fine until I cooked the game and then it exploded.

After debugging it I found that doing it this other way didn’t cause any exceptions.
TMap<UClass*, int32> MyDic;

But I’m not sure if using pointers as dictionary keys is a good idea.

I’m thinking maybe I can do an enumeration and return a static class instead.
Something like this:
TMap<EClassType, int32> MyDic;


SubclassOf<ASomeBase> GetClass( EClassType ClassType)
{
      if(ClassType ==  EClassType::Type1) return AType1::StaticClass();
      if(ClassType ==  EClassType::Type2) return AType2::StaticClass();
...
}

I’m not sure if it would be necessary to do this last one.

Well, if someone with more experience than me could give me their opinion on this I would greatly appreciate it.

I am also open to other possible methods if they are better than these.

Thank you so much!!

The best way is to draw lessons from the engine source code.

2 Likes

Definitely the best answer!!
Thank you so much @MIkasa1009

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.