Download

UE4 C++ Namespace and multiple enumerators

Hello,
When trying to use namespaces and having multiple enums within one namespace an error shows up as
error : Missing ‘}’ in ‘Enum’

Here is the code in question,



UENUM(BlueprintType)
namespace EQuestSystem
{
    enum QuestName
    {
        SubQuest                    UMETA(DisplayName = "An unusable quest. Make sure that the following enums match the ones you use in the spreadsheet!"),
    };

    enum CheckType
    {
        Solo  UMETA(DisplayName = ""),
        Group UMETA(DisplayName = "")
    };


    enum QuestStatus
    {
        Unstarted UMETA(DisplayName = "The quest has not been located."),
        Available UMETA(DisplayName = "The quest has at least been located, or is active."),
        Completed UMETA(DisplayName = "The quest has been completed.")
    };

}

namespace FQuestSystem
{
    USTRUCT(BlueprintType)
    struct MUANDTHELITTLEREEF_API FBannerData
    {
        GENERATED_BODY()

        UPROPERTY(EditAnywhere, BlueprintReadWrite)
        bool bIsStarting;

        UPROPERTY(EditAnywhere, BlueprintReadWrite)
        EQuestSystem::QuestName QuestName;    

    };


Not quite sure why that error occurs. If I only have one enum per namespace then no errors occur, but multiple is when the errors happen. I am pretty sure you can have multiple enums in a namespace in c++ unless Unreal has a different rule about it with the Unrea Build Tool Header.

your 1st enum has a comma, remove it

It may be complaining about where you’re putting the UENUM specifier. Try putting UENUM(BlueprintType) above each enum declaration inside the namespace instead of above the EQuestSystem namespace declaration. See if that works.

Nice thanks for that, that was quite frustrating for a couple of hours.

Update #2
After fighting the Unreal Build Tool, you can’t actually have multiple enums in the namespace due to limited serialization ability of the engine.
Either you have namespace with one enum or no namespace at all.
Quite disappointing, but just have to deal with it.

Reflected types cannot belong to namespaces, UHT does not parse them. Namespaces in general are not really used in UE4. You can actually use namespaced Enums, but you are better off using strongly-typed enums instead otherwise you have to use a wrapper class to expose them.

The UE4 coding standard dictates that you shouldn’t use namespaces for enums, though some legacy enums accross the engine still use them out of neccessity.

This:



UENUM(BlueprintType)
enum class EMyEnum : uint8
{};

UPROPERTY(EditAnywhere)
EMyEnum SomeEnum;


Is far more preferrable to this:



namespace SomeNamespace
{
     enum EMyEnum
     {};
}

UPROPERTY(EditAnywhere)
TEnumAsByte<SomeNamespace::EMyEnum> SomeEnum;