Download

ENums as UPROPERTY?

Hello,

I got stuck at something when i was trying to program an inventory system, was trying to assign a slot to all of the items so they can be equipped differently (primary weapons, armors etc) so i added this in my item code



namespace EItemEquipSlot
{
	enum Type
	{
		eIES_PrimaryWeapon,
		eIES_SecondaryWeapon,
		eIES_MeleeWeapon,
		eIES_Head,
		eIES_Torso,
		eIES_Back,
		eIES_Arm,
		eIES_Waist,
		eIES_Hand,
		eIES_Leg,
		eIES_Foot,
	};
}

UPROPERTY(EditDefaultsOnly, Category = Inventory)
	EItemEquipSlot::Type EquipSlot;

but i get this error


error : In Item: Unrecognized type 'EItemEquipSlot'

meaning that it’s not possible to use ENums directly with UPROPERTY, is there any way to create a sub class for ENums to be used with UPROPERTY or what others ways can i try to do the same thing?

Thanks in advance.

Prefix your enum declaration with UENUM() (before the namespace in this case).

You might also want to wrap the actual property in TEnumAsByte<>.

Cheers,
Michael Noland

Assuming you’re okay using C++11, you don’t need the namespace workaround and can do like this:


UENUM(BlueprintType)
enum class EHeadLookCameraMode: uint8 {
	...
};

The uint8 part is important, as is the BlueprintType specifier if you want to access the property within a blueprint.
Maybe Michael can confirm, but I think assuming you do it this way, TEnumAsByte is no longer required.

Thanks for the help Michael and kamrann! Is there also another way to use structs under a namespace?

Something like:

But returns an error

EDIT: Using USTRUCT before namespace didn’t work.

UnrealHeaderTool only understands namespaces for the namespaced enum trick; it can’t handle them in any other context.

Cheers,
Michael Noland