Download

How many Enums

Hi,

Is there a way to find out how many elements are defined for an enum? :confused:
My goal is to pull a random enum:

.h


UENUM(BlueprintType)
enum class ESpecialSkillTypes : uint8
{
	SS_None UMETA(DisplayName = "None"),
	SS_Surgeon UMETA(DisplayName = "Surgeon"),
	SS_Interpreter UMETA(DisplayName = "Interpreter"),
	SS_Botanist UMETA(DisplayName = "Botanist"),
	SS_Technician UMETA(DisplayName = "Technician"),
	SS_Boaster UMETA(DisplayName = "Boaster"),
	SS_Athlete UMETA(DisplayName = "Athlete"),
	SS_ToughGuy UMETA(DisplayName = "ToughGuy"),
	SS_Marksman UMETA(DisplayName = "Marksman"),
	SS_LuckyDevil UMETA(DisplayName = "LuckyDevil")
};

ESpecialSkillTypes GetRandomSpecialSkill();

.cpp


ESpecialSkillTypes GetRandomSpecialSkill()
{
	int r;
	r = FMath::RandHelper(9);
	return ESpecialSkillTypes(r);
}

So, how can I get away from the hardcoded “9” ?

Any help is appreciated :slight_smile:

Cheers,
Klaus

Like this:

.h


UENUM(BlueprintType)
enum class ESpecialSkillTypes : uint8
{
	SS_None UMETA(DisplayName = "None"),
	SS_Surgeon UMETA(DisplayName = "Surgeon"),
	SS_Interpreter UMETA(DisplayName = "Interpreter"),
	SS_Botanist UMETA(DisplayName = "Botanist"),
	SS_Technician UMETA(DisplayName = "Technician"),
	SS_Boaster UMETA(DisplayName = "Boaster"),
	SS_Athlete UMETA(DisplayName = "Athlete"),
	SS_ToughGuy UMETA(DisplayName = "ToughGuy"),
	SS_Marksman UMETA(DisplayName = "Marksman"),
	SS_LuckyDevil UMETA(DisplayName = "LuckyDevil"),
	SS_TotalCount
};

ESpecialSkillTypes GetRandomSpecialSkill();

.cpp


ESpecialSkillTypes GetRandomSpecialSkill()
{
	int r;
	r = FMath::RandHelper(ESpecialSkillTypes::SS_TotalCount-1);
	return ESpecialSkillTypes(r);
}

Also, with enum class there’s no need for prefixes in your enums, since you have to use the enum class name to access them so there’s no collision, so it’s fine to use this:


UENUM(BlueprintType)
enum class ESpecialSkillTypes : uint8
{
	None,
	Surgeon,
	Interpreter,
	Botanist,
	Technician,
	Boaster,
	Athlete,
	ToughGuy,
	Marksman,
	LuckyDevil,
	TotalCount
};

ESpecialSkillTypes GetRandomSpecialSkill();

Ah, I see. I just have to tell everybody then “Dont use the ‘TotalCount’ value”.
But thats the best way I guess if I dont want to maintain a constant…

Ah, thats good to know. :slight_smile:
Would I still be able to access the enum values as text? (which would be really hand in this case) ?

Yes, they are generated automatically by the UENUM() macro.