I have a series of methods that take in a pointer of a type of interface so that any data type that is using the interface can be passed as a parameter and have its interface methods called. I have debug statements that use GetDisplayName() to print the object context with the debug info by its parameter doesn’t accept interface types. How do I type cast from my interface to the correct Actor or UObject type it wants?
void UBuffComponent::PrintAllTankBuffs(TScriptInterface<IBuffableInterface> tankRef, float _debugTime){
//* This function is for debugging purposes.
//* Error checking for null pointers.
if(tankRef == nullptr){
GEngine->AddOnScreenDebugMessage(-1, _debugTime, FColor::Red, FString(TEXT("(BuffComponent::PrintAllTankBuffs) tankRef is not a valid type.")));
return;
}
FString msg;
if(tankRef->GetActiveBuffs().Num() == 0){
msg = FString(TEXT("Active buff list for ")) += UKismetSystemLibrary::GetDisplayName((AActor)tankRef) += FString(TEXT(" Contains no items."));
GEngine->AddOnScreenDebugMessage(-1, _debugTime, FColor::Magenta, msg);
return;
}
msg = FString(TEXT("Active buff list for ")) += UKismetSystemLibrary::GetDisplayName((AActor)tankRef) += FString(TEXT(" contains ")) += FString::FromInt(tankRef->GetActiveBuffs().Num()) += FString(TEXT(" items."));
GEngine->AddOnScreenDebugMessage(-1, _debugTime, FColor::Yellow, msg);
//* Get every buff and print to screen.
for(int i = 0; i < tankRef->GetActiveBuffs().Num(); i++){
msg = FString::FromInt(i) += FString(TEXT(" : ")) += UKismetSystemLibrary::GetDisplayName(tankRef->GetActiveBuffs()[i]);
GEngine->AddOnScreenDebugMessage(-1, _debugTime, FColor::Cyan, msg);
}
}
error C2440: 'type cast': cannot convert from 'TScriptInterface' to 'AActor'
CompilerResultsLog:Error: Error S:\DysanianDawnWorkspace\PastryPanzerPanic\Source\PastryPanzerPanic\Private\Buffs\BuffComponent.cpp(43) : error C2665: 'Cast': none of the 2 overloads could convert all the argument types
CompilerResultsLog:Error: Error D:\UnrealEngine\Epic Games\4.14\Engine\Source\Runtime\CoreUObject\Public\UObject\Linker.h(512) : note: could be 'T *Cast<AActor>(FLinker *)'
CompilerResultsLog: Info with
CompilerResultsLog: Info [
CompilerResultsLog: Info T=AActor
CompilerResultsLog: Info ]
CompilerResultsLog:Error: Error D:\UnrealEngine\Epic Games\4.14\Engine\Source\Runtime\CoreUObject\Public\Templates\Casts.h(270) : note: or 'T *Cast<AActor>(const FSubobjectPtr &)'
CompilerResultsLog: Info with
CompilerResultsLog: Info [
CompilerResultsLog: Info T=AActor
CompilerResultsLog: Info ]
CompilerResultsLog:Error: Error S:\DysanianDawnWorkspace\PastryPanzerPanic\Source\PastryPanzerPanic\Private\Buffs\BuffComponent.cpp(43) : note: while trying to match the argument list '(TScriptInterface<IBuffableInterface>)'
If you are using TScriptInterface, you need to call “GetObject” when doing your cast to an Actor or some other UObject based class:
AActor* MyActor = Cast<AActor>(tankref.GetObject());
FString DisplayName = UKismetSystemLibrary::GetDisplayName(MyActor);
You can see an example of this in the GameplayAbilities blueprint library method "ForwardGameplayCueToTarget".