Passing an enum as a function parameter

I’m using Unreal Engine 4.20.2.

Here’s how I am successfully passing an enum to a function for use in a TArray.

In my Item.h file:

UENUM(BlueprintType)	//"BlueprintType" is essential to include 
namespace EInventoryItem {
	enum Item {
		Nothing		UMETA(DisplayName = "Nothing"),
		Battery		UMETA(DisplayName = "Battery"),
		Thing	 	UMETA(DisplayName = "Thing"),
		AnotherThing	UMETA(DisplayName = "Another Thing"),
		RedBadge	UMETA(DisplayName = "Red Badge"),
		BlueBadge	UMETA(DisplayName = "Blue Badge")
	};
}

In my Character.h file:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Inventory)
TArray<TEnumAsByte<EInventoryItem::Item>> InventoryItems;

UFUNCTION(BlueprintCallable, Category = Inventory)
bool AddItemToInventory(TEnumAsByte<EInventoryItem::Item> Item);

Using the namespace-style enums works well for me. I was having a ton of issues trying to pass a TEnumAsByte<EInventoryItem> to a function as a parameter, and converting everything to what you see above worked well for me.

Leave a comment

Your email address will not be published. Required fields are marked *