How can I use custom enums in replicated functions?

I am trying to replicate a function that has a custom enum type as parameter.

.h

UFUNCTION(Server, Reliable, WithValidation)
void ServerSetTeam(TEnumAsByte<EKaoriGameTeam::Type> NewTeam);

.cpp

bool AKaoriTeam::ServerSetTeam_Validate(TEnumAsByte<EKaoriGameTeam::Type>  NewTeam)
{
	return true;
}

void AKaoriTeam::ServerSetTeam_Implementation(TEnumAsByte<EKaoriGameTeam::Type>  NewTeam)
{
	Team = NewTeam;
}

If I try to build my game I get the following error

19> …\KaoriTeam.cpp(48): error C2511: ‘bool AKaoriTeam::ServerSetTeam_Validate(TEnumAsByte)’ : overloaded member function not found in ‘AKaoriTeam’
19> …\KaoriTeam.h(15) : see declaration of ‘AKaoriTeam’
19> …\KaoriTeam.cpp(53): error C2511: ‘void AKaoriTeam::ServerSetTeam_Implementation(TEnumAsByte)’ : overloaded member function not found in ‘AKaoriTeam’
19> …\KaoriTeam.h(15) : see declaration of ‘AKaoriTeam’
19> …\Kaori.generated.inl(116): error C2511: ‘void AKaoriTeam::ServerSetTeam(EKaoriGameTeam::Type)’ : overloaded member function not found in ‘AKaoriTeam’
19> …\KaoriTeam.h(15) : see declaration of ‘AKaoriTeam’

If I only change the parameter from TEnumAsByte to lets say uint32 everything works fine.


EDIT: Ok I removed the TEnumAsByte and only use EKaoriGameTeam::Type now it works but at some point the compiler told me that I should use TEnumAsByte if I want to use my Enum, When do I need TEnumAsByte and when not?

Maybe that is not really a network error but a UBT error?

19> …\KaoriTeam.h(15) : see declaration of ‘AKaoriTeam’ 19> …\Kaori.generated.inl(116): error C2511: ‘void AKaoriTeam::ServerSetTeam(EKaoriGameTeam::Type)’ : overloaded member function not found in ‘AKaoriTeam’

there is no TEnumAsByte<> only EKaoriGameTeam::Type

#Epic Help

This sounds like something networking team should look at and see if TEnumAsByte can be supported properly.

You should be using TEnumAsByte wherever possible as it is the Blueprint-friendly type and is a really nice class. Check out EnumAsByte.h !

My favorite is:

/** Get the value */
TEnum GetValue() const
{
	return TEnum(Value);
}

You have a byte but you also have type protection and custom operator equality :slight_smile:

Rama

1 Like