I have a few questions about how I should approach problems. I usually would just solve them how I think they would be solved best but I think there is some value in approaching problems the same way that epic does.
1.) Casting everywhere, avoiding interfaces?
This code is from the SurvivalGame and it looks like
void ASConsumableActor::OnUsed(APawn* InstigatorPawn)
{
// Plays pickup sound from base class
Super::OnUsed(InstigatorPawn);
ASCharacter* Pawn = Cast<ASCharacter>(InstigatorPawn);
if (Pawn)
{
Pawn->ConsumeFood(Nutrition);
}
// Remove from level
Destroy();
}
I don’t want to pick on this code but I saw this also several times in the gameplay framework myself. It seems UE is avoiding interfaces as much as possible and just does a simple cast like
ASCharacter* Pawn = Cast<ASCharacter>(InstigatorPawn);
Do you know why? Simplicity?
The problem I see with this code is that if I ever decide to create another playable character in the future I would have to change code in every ConsumeableAactor.
I probably would have written something like this
void ASConsumableActor::OnUsed(AActor* Instigator)
{
// Plays pickup sound from base class
Super::OnUsed(Instigator);
IFood* Food= Cast<IFood>(Instigator);
if (Food)
{
Food->ConsumeFood(Nutrition);
}
// Remove from level
Destroy();
}
I also thought about using components which gives me the ability to add functionality at runtime, it also encapsulates the implementation and makes it reusable.
What would you recommend?
2.) NULL vs nullptr
I see code a lot that looks something like this
ASCharacter* MyPawn = PCOwner ? Cast<ASCharacter>(PCOwner->GetPawn()) : NULL;
But UE also uses nullptr in some places
template <typename From, typename To, ECastType CastType = TGetCastType<From, To>::Value>
struct TCastImpl
{
// This is the cast flags implementation
FORCEINLINE static To* DoCast( UObject* Src )
{
return Src->GetClass()->HasAnyCastFlag(TCastFlags<To>::Value) ? (To*)Src : nullptr;
}
};
So I think Epic embraces nullptr over NULL now?
Update It seems it is already documented
3.) What is the meaning of ‘FFoo’?
Most other classes are prefixed by F, though some subsystems use other letters
Just out of curiosity, what does F stand for? Fixed?