I have a Component that I attach to projectiles spawned by my character. I would like to make a callback function that is called when the projectile hits a valid target.
In Component.h I declare a function :
template<class T>
void OnTargetHit( void (T::*Callback)() );
In Component.cpp is implementation:
template<class T>
void UInterpolationComponent::OnTargetHit( void (T::*Callback)() )
{
(T::*Callback)();
}
Now in my Character.cpp I would like to Get the component and assign it a "callback" function, a function that would be triggered when te target is hit, ofcourse this component could be applied to different Actors, therefore I want to pass a function from the calling class so I provide a specific implementation:
AActor* Projectile = GetWorld()->SpawnActor<AActor>(PrimaryAttackProjectile_BP, GetActorLocation(), Rotation);
UInterpolationComponent* InterpComp = Cast<UInterpolationComponent>(Projectile->GetComponentByClass(UInterpolationComponent::StaticClass()));
InterpComp->InterpSpeed = 1800.f;
InterpComp->bHoming = false;
InterpComp->StaticTarget = Location;
InterpComp->bIsDestroyable = true;
InterpComp->OnTargetHit<ACartagraCharacter>(&CalculateDamage);
The problem is with the last line of code :
InterpComp->OnTargetHit<ACartagraCharacter>(&CalculateDamage);
I'm not sure if I should pass an ampersand or not, If I should Include the class name, I tried all of the following:
InterpComp->OnTargetHit<ACartagraCharacter>(CalculateDamage);
InterpComp->OnTargetHit<ACartagraCharacter>(&ACartagraCharacter::CalculateDamage);
None of them work, either gives me an ampersand error, or if in the last case presented it gives me a linker error. I also tried making them static functions, but that doesn't make too much sense as the component is instanced on each spawned projectile. I'm new to template functions and not sure how I'm supposed to do this. Please help.