Error while following the First Person Shooter Tutorial

Hi. I’m very new to C++ and while following the First Person Shooter tutorial on unrealengine.com, I’ve come across this line in the file FPSProjectile.cpp:


CollisionComponent->OnComponentHit.AddDynamic(this, &AFPSProjectile::OnHit);

which throws the following error message:

C:\Users\CurrentUser\Documents\Unreal Projects\FPSProject\Source\FPSProject\FPSProjectile.cpp(15): error C2664: ‘void TBaseDynamicMulticastDelegate<FWeakObjectPtr,void,UPrimitiveComponent *,AActor *,UPrimitiveComponent *,FVector,const FHitResult &>::__Internal_AddDynamic<AFPSProjectile>(UserClass ,void (__cdecl AFPSProjectile:: )(UPrimitiveComponent *,AActor *,UPrimitiveComponent ,FVector,const FHitResult &),FName)': cannot convert argument 2 from 'void (__cdecl AFPSProjectile:: )(AActor *,UPrimitiveComponent ,FVector,const FHitResult &)’ to 'void (__cdecl AFPSProjectile:: )(UPrimitiveComponent *,AActor *,UPrimitiveComponent *,FVector,const FHitResult &)’
with

          UserClass=AFPSProjectile
      ]

C:\Users\CurrentUser\Documents\Unreal Projects\FPSProject\Source\FPSProject\FPSProjectile.cpp(15): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

I’ve checked and double-checked and I’m sure I have reproduced all the code exactly as it is in the tutorial. Could someone explain that error message to me please?

I’m using Unreal Editor 4.13.1 and Visual Studio Community 2015.

Hello and welcome to the forums.

You’ve done nothing wrong. The tutorial has been written using an older Unreal Engine version so some things have changed over time.

In your header file the signature of the OnHit function looks probably like this:


UFUNCTION()
void OnHit(AActor * Actor, UPrimitiveComponent * PrimitiveComponent, FVector Vector, const FHitResult & HitResult);

However the signature expected by the OnComponentHit delegate has changed so your code should instead look like this:


UFUNCTION()
void OnHit(UPrimitiveComponent * PrimitiveComponent1, AActor * Actor, UPrimitiveComponent * PrimitiveComponent2, FVector Vector, const FHitResult & HitResult);

Goes without saying that you’ll also need to change the implementation.

In order that you’ll be able to fix other potential errors of this type the compiler error message essentially boils down to “cannot convert argument 2 from ‘void (__cdecl AFPSProjectile::* )(AActor *,UPrimitiveComponent ,FVector,const FHitResult &)’ to 'void (__cdecl AFPSProjectile:: )(UPrimitiveComponent *,AActor *,UPrimitiveComponent *,FVector,const FHitResult &)’” if you ignore everything else for the moment. While this is the error message it also gives you a hint to fix it.

Excellent! That did indeed solve the error and your “boiled down” version of the error message was much more intuitive and a great help.
Many thanks.