Do TMap and RPC's support Enum Classes?

Hello!

I have difficulties trying to use C++ 11 enum class feature in my current project.
This is the enum that I’ve created:

 UENUM()
enum class WeaponID : uint8 {

	Pistol,
	Shotgun,
	SniperRifle,
	AssaultRifle
};

Problem #1:

When I declare this member variable in one of my classes:

TMap<WeaponID, AWeapon*> Weapons;

I get this:

F:\Epic Games\4.5\Engine\Source\Runtime\Core\Public\Containers\Map.h(158): error C2665: 'TSharedRef<ISlateMetaData,0>::GetTypeHash' : none of the 241 overloads could convert all the argument types
1>          F:\Epic Games\4.5\Engine\Source\Runtime\Core\Public\Templates\SharedPointer.h(397): could be 'uint32 TSharedRef<ISlateMetaData,0>::GetTypeHash(const TSharedRef<ISlateMetaData,0> &)' [found using argument-dependent lookup]
1>          F:\Epic Games\4.5\Engine\Source\Runtime\Core\Public\Templates\SharedPointer.h(397): or       'uint32 TSharedRef<FSlateWindowElementList::FDeferredPaint,0>::GetTypeHash(const TSharedRef<FSlateWindowElementList::FDeferredPaint,0> &)' [found using argument-dependent lookup]
1>          f:\epic games\4.5\engine\source\runtime\core\public\uobject\WeakObjectPtrTemplates.h(178): or       'uint32 TWeakObjectPtr<ABrush,FWeakObjectPtr,FIndexToObject>::GetTypeHash(const TWeakObjectPtr<ABrush,FWeakObjectPtr,FIndexToObject> &)' [found using argument-dependent lookup]
1>          F:\Epic Games\4.5\Engine\Source\Runtime\Core\Public\Containers\StaticArray.h(107): or       'uint32 TStaticArray<T,7,16>::GetTypeHash(const TStaticArray<T,7,16> &)' [found using argument-dependent lookup]
1>          with
1>          [
1>              T=FVector4
1>          ]
...

Once I replace WeaponID with uint8 in this line:

TMap<WeaponID, AWeapon*> Weapons;

the error goes away.

Why can’t I use my cutom enum as key in a TMap?

Problem #2
If I am trying to compile code containing this function:

UFUNCTION(Reliable, NetMulticast)
	void SetActiveWeapon(WeaponID Weapon);

I get a bunch of these:

1>f:\\projects\gungame\ue4\trunk\merry\source\merry\MerryCharacter.h(23): error C3431: 'WeaponID' : a scoped enumeration cannot be redeclared as an unscoped enumeration

Does anyone know what is causing the problem?

Any advice would be highly appreciated!

Thanks!

.

It has to do with how TMap is setup. Everything must somehow be converted to a basic type for comparison (in this case I chose uint8). What I did is right below my enum put a version of the GetTypeHash function to facilitate conversion to the basic type. Annoying, but enums are technically a new type. Alternatively you can remove the word class turning it into a standard enum instead, which also seems to work.

enum class EGameKey : uint8
{
	Tap,
	Hold,
	Swipe,
	SwipeTwoPoints,
	Pinch,
};

// so TMap will work properly
inline uint8 GetTypeHash(const EGameKey A)
{
	return (uint8)A;
}

Thank you, I was just trying to figure the same thing out :slight_smile:

I still can’t get this to work with TMap. I am seeing the below errors.
Any chance someone can show a complete examples as to where inline uint8 GetTypeHash(const EGameKey A) is put in the header file?

C2065: ‘EStat’ : undeclared identifier

C2923: ‘TMap’ : ‘EStat’ is not a valid template type argument for parameter

If your enum is not EGameKey, then you need to change the inline function to be inline uint8 GetTypeHash(const EStat A)... instead. It’s hard to know the exact issue without knowing where and how you have the two defined.

I actually made a new post and was able to get it resolved. If your interested https://answers.unrealengine.com/questions/314630/new-enum-impenemtation-with-tmap.html