Having a heck of a time creating an enum- functional enum declaration stopped working

So I have an enum class ECharacterState I’ve been using to store data on the player state: attacking, moving, and so forth. It worked perfectly, and let me create and read/write variables of type ECharacterState with no trouble. I’m refactoring my code, and since the only place this enum is used is in the base character class, I tried deleting it from the class it was originally in, and pasting the declaration into my BaseCharacter header:


#pragma once

#include "GameFramework/Character.h"
#include "MBaseCharacter.generated.h"

UCLASS()
class CODEPROJ_API AMBaseCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AMBaseCharacter(const class FObjectInitializer& ObjectInitializer);

	UFUNCTION(BlueprintCallable, Category = "PlayerCondition")
	ECharacterState characterState;

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
};

UENUM(BlueprintType)
enum class ECharacterState : uint8{
	Locomotion,
	Attacking,
	Flinching,
	Nonresponsive,
	Menu,
};

This looks absolutely right to me, and I checked it against the example syntax from A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums a bunch of times, but it won’t compile anymore- now every time I try, it gives me an error on the line “ECharacterState characterState;”, "Error: Unrecognized type ‘ECharacterState’ ".

Is it obvious what I messed up moving it between classes? I can’t seem to figure this out. :frowning:

This is really simple actually, you have your enum declaration **after **the character which uses it :slight_smile: Switch them around and you should be fine.

Ooh, I’m going to feel really silly if this works. :slight_smile: You mean like this, right?


#pragma once

#include "GameFramework/Character.h"
#include "MBaseCharacter.generated.h"

UENUM(BlueprintType)
enum class ECharacterState : uint8{
	Locomotion,
	Attacking,
	Flinching,
	Nonresponsive,
	Menu,
};

UCLASS()
class CODEPROJ_API AMBaseCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AMBaseCharacter(const class FObjectInitializer& ObjectInitializer);

	UFUNCTION(BlueprintCallable, Category = "PlayerCondition")
	ECharacterState characterState;

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
};

That gets rid of the type error, but now it pops a different error, “Bad function definition”, for my “ECharacterState characterState;” line. I don’t need to add an #include for the enum or something, do I?

hmm i always type up enums like this.


UENUM(BlueprintType)
namespace ECharacterState
{
	enum State 
	{	
		Locomotion	UMETA(DisplayName = "Display Name in Editor"),
		Attacking	UMETA(DisplayName = "Display Name in Editor"),
		Flinching	UMETA(DisplayName = "Display Name in Editor"),
		Nonresponsive	UMETA(DisplayName = "Display Name in Editor"),
		Menu		UMETA(DisplayName = "Display Name in Editor")
	};
}


And in a class i do like this.


UPROPERTY()
	TEnumAsByte<ECharacterState::State > PlayerState;


This compiles and run fine.

Hope it helps.

Yeah, that is quite easy. An Enum is a variable and not a function :smiley: You may want to remove the UFUNCTION macro and use a UPROPERTY macro.

Awww, I figured it out on my own and came to post about it, and you just beat me to the punch- thank you guys! :slight_smile:

I ended up solving it by systematically commenting out everything until it started compiling, but that was a really silly error to be making- correct macro usage for the win. ^^

Yeah, this is correct too, this is how you would declare enums in pre-C++11 era.