How can I properly access an Enum Class from another class?

How can I properly access an Enum Class from another class?

For example, I have:

UENUM(BlueprintType) enum class EDirection : uint8 { UP, DOWN, LEFT, RIGHT, END };

In ASampleClass, how can I access it in UObjectSample class??

Thank you!

Can I do this in the header file of UObjectSample?
Also, would it be okay if i
#include “ObjectSample.h” in the header of SampleClass.h?
I know this is a noobish question but I’m still trying to get around UE4 c++ programming. :confused:
Thanks! I cant test right now, since I’m away. I’ll try it later.

In UObjectSample.cpp

#include "SampleClass.h"

ASampleClass::EDirection::UP;

Something like this?

Hope this helps,
Elias

In the header you should foreward declarate:

at the top of .h:

#include "SampleClass.h" // not sure if this is needed
enum EDirection;

edit: struct → enum ofcourse

oh whoops

Enums can’t be forward declared, so you’ll have to make a separate .h file where you declare your enum and include that one in all headers where you want to use them.

Sorry, it got me confused also :slight_smile:

Have you tried to use the keyword “extern”?

A separate header file would be more appropriate! One last question though, of course I can directly create a header file in visual studio directly right? What I want to know is what object should that header file inherit? Should it be a UObject? Or it doest need to anymore and I can just put the enum class in there directly? Thanks again!

Hey Jibera,

The header file with enums does not have to inherit anything, ill give you an example:
My file is called MyEnums.h

#include "MyEnums.generated.h"
#pragma once

UENUM()
enum class ECount: uint8 {
	one,
	two,
	three,
};

UENUM()
    enum class ESecondEnum: uint8 {
    	four,
    	five,
    	six,
    };

It does not even need a .cpp file, just include the header anywhere you want and you can use the enums in here :slight_smile:

holy crap! It freakin worked! Will tag this as an answer. Thank you so much!

thanks bro. it also worked for me.

Guide to understand and useful. They are very helpful to me. thanks!
among us 2