Enum parameter with UFUNCTION

I have an Enum.h that is included in my project header (so that every class automatically includes it):

#pragma once

UENUM(BlueprintType)
enum class ETeam : uint8
{
	None		UMETA(DisplayName = "None"),
	Players		UMETA(DisplayName = "Players"),
	Neutrals	UMETA(DisplayName = "Neutrals"),
	Enemies		UMETA(DisplayName = "Enemies")
};

Now I have another class (AmmoReserve.h) with this in the header:

UFUNCTION(BlueprintCallable, Category = "AmmoReserve")
bool CanDamageTeam(ETeam Team);

The above code generates a compile-time error (probably from the UE4 header-tool or something, not from VS itself):

AmmoReserve.h(30) : Unrecognized type
'ETeam ’ - type must be a UCLASS,
USTRUCT or UENUM

Mmmm
 Okay but this works:

UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "AmmoReserve")
ETeam Team;

And this also works (no UFUNCTION):

bool CanDamageTeam(ETeam Team);

So the class DOES know the ETeam enum but not in combination with with a UFUNCTION macro
 After a lot more testing I found out that using a manual include in the AmmoReserve.h fixes it:

include “Enums.h”

So it seems that for UFUNCTION the UE4 headertool (or whatever tool is used) does NOT check the global project header:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Engine.h"
#include "UnrealNetwork.h"

// Custom includes
#include "Lib.h"
#include "Enums.h" // <<<<<<<<<<<<<<<<<<<<<<< included here! Why do I still require a manual include...
#include "Structs.h"
#include "GameStatics.h"

// Custom Macro's
//<snip>
1 Like

Hey -

When you create a new class, the source file for the class includes the project’s header file but the class header does not have this include statement. If you have your Enum in its own class that is included in your project header, you will need to add an include statement for either the Enum class or the project header to the new class header.

Cheers

So this is intended behavior?

but the class header does not have this include statement

Then why does it work for UPROPERTY if it doesn’t even know the enum at all? Because it seems it does know it and it seems it does somehow include the project-header-file (which contains the enums and such) because otherwise UPROPERTY can not work right? Same for structs and everything, those also work.

It’s just UFUNCTION + enum in global class + project header include = compiler error. Any other combinations work including structs.

UPROPERTY + enum in global class + project header include = works fine.

enum in global class + project header include = works fine.

UFUNCTION + struct in global class + project header include = works fine.

Etc.

Because of this extremely rare exception to the rule, I find it so hard to believe that this is intended behavior. And if it is intended, can someone perhaps explain me the design reasons behind this?

I’m sorry, I misunderstood the question. I didn’t realize you were referring to the difference in behavior. I have entered a bug report, UE-31308 , for investigation.

Encountered the same problem using Structs:

Basically the Struct (located in another file and included through the project-header) is valid and works in both blueprints and C++. But the moment I put it in a UPROPERTY.

UE-31308 was closed as Won’t Fix on February 5th.

We’ve encountered numerous varieties of this “type must be a UCLASS, USTRUCT or UENUM” problem when it is correctly included. We would like to see this fixed.

1 Like

Hey -

This issue was initially closed due to inactivity on the bug. I have reopend the report added a note that this is still affecting people to provide more visibility to the issue.

Ran into the same situation right now. Cause of circular dependencies i had to move the header include inside the MyGame.h. Had never had problems with it before. Kinda sucks, have to find a way around it. Just dont want to mess up the classes with all the structs and enums.

Could you make an example for UFUNCTION,I am a new learner