KVogler
(KVogler)
November 25, 2016, 2:04pm
1
Hi,
Is there a way to find out how many elements are defined for an enum?
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
Cheers,
Klaus
manoelneto
(manoelneto)
November 25, 2016, 5:09pm
2
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();
1 Like
KVogler
(KVogler)
November 25, 2016, 5:20pm
3
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ā¦
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:
Ah, thats good to know.
Would I still be able to access the enum values as text? (which would be really hand in this case) ?
manoelneto
(manoelneto)
November 25, 2016, 5:37pm
4
Yes, they are generated automatically by the UENUM() macro.