I’m trying to bind my function to a delegate using AddDynamic. However, I keep getting a ‘error C2664’ that states:
no instance of function template "FComponentHitSignature::__Internal_AddDynamic" matches the argument list -- C/C++ (304) [Ln 29, Col 33]
argument types are: (AProjectile *, void (AProjectile::*)(UPrimitiveComponent *HitComp, AActor *OtherActor, UPrimitiveComponent *OtherComp, FVector NormalImpulse, FHitResult &Hit), FName)
object type is: FComponentHitSignature
I’ve tried restarting the project, rebuilding the code while project is closed. Checked to see if the delegate is a UFUNCTION(). I’m not sure why I’m getting this error.
Has anyone run into this issue before?
Here is my header & CPP file:
Header
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Projectile.generated.h"
UCLASS()
class TOONTANKS_API AProjectile : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AProjectile();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Combat", meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* ProjectileMesh;
UPROPERTY(VisibleAnywhere, Category = "Movement")
class UProjectileMovementComponent* ProjectileMovement;
UPROPERTY(EditAnywhere, Category = "Movement")
float InitialSpeed;
UPROPERTY(EditAnywhere, Category = "Movement")
float MaxSpeed;
// Callback function for a hit event
// This type of callback needs certain parameters. First, a UPrimitiveComponent* HitComp (This will handle the collision)
// Next will be AActor* OtherActor to represent the object we collide with
// Next will be another UPrimitiveComponent* OtherComp. This is the other component that was it
// Next will be a FVector NormalImpulse to simulate physics and apply an impulse to a collision (directiohn & Maginitude)
// Finally we will need a const FHitResult& Hit. This will return information about the hit
// Because this is a delegate for a hit funciton, it has to be a UFUNCTION() for it to work
UFUNCTION()
void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, FHitResult& Hit);
};
CPP
#include "Projectile.h"
#include "Components/StaticMeshComponent.h"
// Sets default values
AProjectile::AProjectile()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// Creating Constructor so that I can edit and see this in Blueprint
ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Projectile Mesh"));
RootComponent = ProjectileMesh;
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement Component"));
ProjectileMovement->MaxSpeed = 1300.f;
ProjectileMovement->InitialSpeed = 1300.f;
}
// Called when the game starts or when spawned
void AProjectile::BeginPlay()
{
Super::BeginPlay();
// Binding the OnHit() function to our delegate
// Doing this in the constructor can cause issues (e.g. not binding the function to the delegate)
ProjectileMesh->OnComponentHit.AddDynamic(this, &AProjectile::OnHit);
}
// Called every frame
void AProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Callback Function that is defining the Hit Event
void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, FHitResult& Hit)
{
UE_LOG(LogTemp, Warning, TEXT("Hit"));
}