Hi there,
I am currently working through this tutorial https://wiki.unrealengine.com/First_Person_Shooter_C%2B%2B_Tutorial and I have made it to the section called “Projectiles Interacting with the World”.
The problem I am having is that when i type the line:
CollisionComp->OnComponentHit.AddDynamic(this, &AFPSProjectile::OnHit);
I get an error that says
No instance of function template "FComponentHitSignature::__Internal_AddDynamic" matches the argument list
argument types are: (AFPSProjectile*, void (AFPSProjectile::*)(AActor *OtherActor, UPrimitiveComponent *OtherComp, FVector NormalImpulse, cosnt FHitResult &Hit), FName)
Object type is: FComponentHitSignature
FPSProjectile.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "FPSProjectile.generated.h"
UCLASS()
class FPSPROJECT_API AFPSProjectile : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFPSProjectile();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
// Sphere collision component
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
USphereComponent* CollisionComp;
// Projectile movement component
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement)
UProjectileMovementComponent* ProjectileMovement;
AFPSProjectile(const FObjectInitializer& ObjectInitializer);
// Inits velocity of the projectile in the shoot direction
void InitVelocity(const FVector& ShootDirection);
// called when projectile hits something
UFUNCTION()
void OnHit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
};
FPSProjectile.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "FPSProject.h"
#include "FPSProjectile.h"
#include "UObject.h"
// Sets default values
AFPSProjectile::AFPSProjectile()
{
// 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;
}
// Called when the game starts or when spawned
void AFPSProjectile::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AFPSProjectile::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
AFPSProjectile::AFPSProjectile(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
// Use a sphere as a simple collision representation
CollisionComp = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("SphereComp"));
CollisionComp->BodyInstance.SetCollisionProfileName("Projectile");
CollisionComp->OnComponentHit.AddDynamic(this, &AFPSProjectile::OnHit);
CollisionComp->InitSphereRadius(15.0f);
RootComponent = CollisionComp;
// Use a ProjectileMovementComponenet to govern this projectile's movement
ProjectileMovement = ObjectInitializer.CreateDefaultSubobject<UProjectileMovementComponent>(this, TEXT("ProjectileComp"));
ProjectileMovement->UpdatedComponent = CollisionComp;
ProjectileMovement->InitialSpeed = 3000.f;
ProjectileMovement->MaxSpeed = 3000.f;
ProjectileMovement->bRotationFollowsVelocity = true;
ProjectileMovement->bShouldBounce = true;
ProjectileMovement->Bounciness = .3f;
}
void AFPSProjectile::InitVelocity(const FVector& ShootDirection)
{
if (ProjectileMovement)
{
// Set the projectile's velocity to the desired direction
ProjectileMovement->Velocity = ShootDirection * ProjectileMovement->InitialSpeed;
}
}
void AFPSProjectile::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if (OtherActor && (OtherActor != this) && OtherComp)
{
OtherComp->AddImpulseAtLocation(ProjectileMovement->Velocity * 100.0f, Hit.ImpactPoint);
}
}
I’ve spent a ton of time researching and I’m not sure what to do, I can’t figure out why I seem to be the only one having this problem. I’ve read about the event not firing, but I am unable to even get it to compile. It’s probably a small typo but I’ve retyped it and still it doesn’t work.
Thank you.