How to create a globally-available enum?

Hey,

I want to create a enum that contains the different types of weapons I will have (handgun, rifle, melee, etc). But if I define this enum in the c++ weapon class, my pawn’s animation blueprint can’t access it. (I need my animblueprint to access it, because I need to have the pawn doing a different animation depending on the current weapon type.)

So where can I define an enumeration so that everything, anywhere, can see it? I’m using C++, so I can’t declare one in the content browser. I also tried declaring it in MyProject.h, but that gave me errors. (?)

Thanks in advance :slight_smile:

Hi Greg,

If you mark your enum declaration with UENUM(BlueprintType), then the enum should be usable in any Blueprints, including Anim Blueprints.

Cheers,
Michael Noland

Copy and Paste the code that you put in the MyProject.h, and the errors you got.
Are you using UENUMTYPE()?
's a tutorial (scroll down to the C++ code): What is proper way to make Enum for BP and C++ both - Programming & Scripting - Epic Developer Community Forums

@Micheal Noland, yep, it shows up in blueprints now! Thanks :slight_smile:

@jwatte, 's my GB.h code (GB is the name of my project):

[HR][/HR]
#ifndef GB_H
#define GB_H

#include “Engine.h”

#define COLLISION_PROJECTILE ECC_GameTraceChannel1

#endif

#include “GB.generated.h”

UENUM(BlueprintType)
enum EItemAnimType
{ None UMETA(DisplayName = “None”),
Rifle UMETA(DisplayName = “Rifle”),
Handgun UMETA(DisplayName = “Handgun”),
Melee UMETA(DisplayName = “Melee”)
};
[HR][/HR]
And I get this huge wall of errors:

c:\users\greg\documents\unreal projects\gb\intermediate\build\win64\inc\gb../…/…/…/…/Source/GB/GB.h(26): error C2011: ‘EItemAnimType’ : ‘enum’ type redefinition
1> c:\users\greg\documents\unreal projects\gb\intermediate\build\win64\inc\gb../…/…/…/…/Source/GB/GB.h(26) : see declaration of ‘EItemAnimType’
1>c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26): error C2011: ‘EItemAnimType’ : ‘enum’ type redefinition
1> c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’
1>c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26): error C2011: ‘EItemAnimType’ : ‘enum’ type redefinitionc:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : error C2011: ‘EItemAnimType’ : ‘enum’ type redefinition
1>
1> c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’ c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’
1>
1> GBUIWidget.cpp
1>c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26): error C2011: ‘EItemAnimType’ : ‘enum’ type redefinition
1> c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’
1> GBPants.cpp
1>c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26): error C2011: ‘EItemAnimType’ : ‘enum’ type redefinition
1> c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’
1> GBShirt.cpp
1> GBClothing.cpp
1>c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26): error C2011: ‘EItemAnimType’ : ‘enum’ type redefinition
1> c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’
1>c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26): error C2011: ‘EItemAnimType’ : ‘enum’ type redefinition
1> c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’
1> GBBackpack.cpp
1>c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26): error C2011: ‘EItemAnimType’ : ‘enum’ type redefinition
1> c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’
1> GBWeapon.cpp
1> GBInventoryManager.cpp
1> GBInventoryItem.cpp
1>c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26): error C2011: ‘EItemAnimType’ : ‘enum’ type redefinitionc:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : error C2011: ‘EItemAnimType’ : ‘enum’ type redefinition
1>
1> c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’ c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’
1>
1>c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26): error C2011: ‘EItemAnimType’ : ‘enum’ type redefinition
1> c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’
1> GBProjectile.cpp
1>c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26): error C2011: ‘EItemAnimType’ : ‘enum’ type redefinition
1> c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’
1> GB.cpp
1>c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26): error C2011: ‘EItemAnimType’ : ‘enum’ type redefinition
1> c:\users\greg\documents\unreal projects\gb\source\gb\GB.h(26) : see declaration of ‘EItemAnimType’

Actually, it’s the same error over and over again. But how do I fix it? :frowning:

They are beeing redefined so if you dont have the same code in two places.
Then your header is beeing included “twice”.

I think your issue may be the inclusion guards.
I think i read somewhere that you need to use the visual studio one.


#pragma once

And it also probeboly be a good idea to create a namespace for them.

Thanks! Fixed it! I noticed that #pragma once is included in most of the source files by default, but not GB.h. Wonder why.

One more thing: is it bad to #include “GB.h” in both the .h and .cpp files for other classes? By default it’s only included in the .cpp, but I want to declare some of my custom enums in the .h.

Heya,

Just like to pop in o share the way I handle enums and structs. This is mostly based off of a tutorial from the wiki by , so credits to him. 's my code:


#pragma once
#include "Archrune.h"
#include "Constants.generated.h"

UCLASS()
class UConstants : public UBlueprintFunctionLibrary
{
	GENERATED_UCLASS_BODY()
};


#pragma region ENUMS

UENUM(BlueprintType)
namespace ELevelState
{
	enum Type
	{
		Locked,
		Open,
		Complete
	};
}


UENUM(BlueprintType)
namespace EPlayerCameraAxisState
{
	enum Type
	{
		Free,
		AttachedRelative,
		AttachedWorld
	};
}

#pragma endregion ENUMS



#pragma region STRUCTS

USTRUCT(BlueprintType)
struct FLevelStruct : public FTableRowBase
{
	GENERATED_USTRUCT_BODY()


	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Level Properties")
	uint8 Level_Index;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Level Properties")
	FText Level_Name;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Level Properties")
	TEnumAsByte<ELevelState::Type> Level_State;
};

#pragma endregion STRUCTS

This allows me to have enums and structs in a single organized file, but also have a bunch of INLINE functions in the header allowing for a sort of GameUtility script. It’s really handy for keeping stuff organized.

2 Likes

@DamirH: This is very usefull thanks for the share it gave me some ideas as well :slight_smile:

#pragma once is poor form.

You actually had almost the right solution already in the original file, except you had the “#endif” for the GB_H in the wrong place – it should be at the end.


#ifndef __GB_H__
#define __GB_H__

#include "Engine.h"

#define COLLISION_PROJECTILE ECC_GameTraceChannel1

// #endif  // this was wrong

#include "GB.generated.h"


UENUM(BlueprintType)
enum EItemAnimType
{
None UMETA(DisplayName = "None"),
Rifle UMETA(DisplayName = "Rifle"),
Handgun UMETA(DisplayName = "Handgun"),
Melee UMETA(DisplayName = "Melee")
};

#endif  // this is what it should have been


Not really… It’s supported by pretty much every noteworthy compiler out there today.

@jwatte, that actually didn’t work for me for some reason, I needed the pragma once for it to work. No matter, “pragma once” compiles, that’s good enough for me :slight_smile:

(Sorry for late reply, internet’s been down.)

The resson for this is that (and am 99.99% sure about this).
That some where in the documentation it says NOT to use inclusion guards like #ifndef FILE_H
But use the #pragma once i reserve the right to be wrong but am prety sure about it.

Am sure a UE 4 Dev drops by soon enofe and confirm this or not :wink: