I have been following the FPS tutorial here When I get to the projectile interacting with the world section it tells me to the following line of code:
CollisionComp->OnComponentHit.AddDynamic(this, &AFPSProjectile::OnHit);
The AddDynamic function isnt there for me I can only call __Internal_AddDynamic which doesnt work.
So how can I call my OnHit function when I collide with an object?
Hi Luciuspain,
What happens if you AddDynamic manually without using the autocomplete and compile. Does it throw up an error?
-W
Yeah that worked thanks. I have been spoiled by autocomplete so I automatically assume if it doesnt show up in autocomplete its not there. Hopefully I wont make that mistake again.
hi, I have the same problem. AddDynamic don’t work.
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* CollisionComponent;
// Projectile movement component.
UPROPERTY(VisibleAnywhere, Category = Movement)
UProjectileMovementComponent* ProjectileMovementComponent;
// Function that initializes the projectile's velocity in the shoot direction.
void FireInDirection(const FVector& ShootDirection);
// Function that is called when the projectile hits something.
UFUNCTION()
void OnHit(AActor* OtherActor, 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"
// 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;
// Use a sphere as a simple collision representation.
CollisionComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
CollisionComponent->BodyInstance.SetCollisionProfileName(TEXT("Projectile"));
CollisionComponent->OnComponentHit.AddDynamic(this, &AProjectile::OnHit);
// Set the sphere's collision radius.
CollisionComponent->InitSphereRadius(15.0f);
// Set the root component to be the collision component.
RootComponent = CollisionComponent;
// Use this component to drive this projectile's movement.
ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
ProjectileMovementComponent->SetUpdatedComponent(CollisionComponent);
ProjectileMovementComponent->InitialSpeed = 3000.0f;
ProjectileMovementComponent->MaxSpeed = 3000.0f;
ProjectileMovementComponent->bRotationFollowsVelocity = true;
ProjectileMovementComponent->bShouldBounce = true;
ProjectileMovementComponent->Bounciness = 0.3f;
// Die after 3 seconds.
InitialLifeSpan = 3.0f;
}
// Called when the game starts or when spawned
void AFPSProjectile::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AFPSProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Function that initializes the projectile's velocity in the shoot direction.
void AFPSProjectile::FireInDirection(const FVector& ShootDirection)
{
ProjectileMovementComponent->Velocity = ShootDirection * ProjectileMovementComponent->InitialSpeed;
}
// Function that is called when the projectile hits something.
void AFPSProjectile::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
if (OtherActor != this && OtherComponent->IsSimulatingPhysics())
{
OtherComponent->AddImpulseAtLocation(ProjectileMovementComponent->Velocity * 100.0f, Hit.ImpactPoint);
}
}
these are the errors:
Errore C2664 ‘void TBaseDynamicMulticastDelegate<FWeakObjectPtr,void,UPrimitiveComponent *,AActor *,UPrimitiveComponent *,FVector,const FHitResult &>::__Internal_AddDynamic(UserClass ,void (__cdecl AFPSProjectile:: )(UPrimitiveComponent *,AActor *,UPrimitiveComponent ,FVector,const FHitResult &),FName)’: impossibile convertire l’argomento 2 da 'void (__cdecl AFPSProjectile:: )(AActor *,UPrimitiveComponent ,FVector,const FHitResult &)’ a 'void (__cdecl AFPSProjectile:: )(UPrimitiveComponent *,AActor *,UPrimitiveComponent *,FVector,const FHitResult &)’ FPSProject C:\Users\mrcso\Desktop\Marco[IMPORTANTE]–Studio–\UnrealEngine\ProveQuickProgramming\FPSProject\Source\FPSProject\FPSProjectile.cpp
Errore Failed to produce item: C:\Users\mrcso\Desktop\Marco[IMPORTANTE]–Studio–\UnrealEngine\ProveQuickProgramming\FPSProject\Binaries\Win64\UE4Editor-FPSProject-2933.dll FPSProject C:\Users\mrcso\Desktop\Marco[IMPORTANTE]–Studio–\UnrealEngine\ProveQuickProgramming\FPSProject\Intermediate\ProjectFiles\ERROR 1
Errore MSB3075 uscita del comando ““C:\Program Files (x86)\Epic Games\4.13\Engine\Build\BatchFiles\Build.bat” FPSProjectEditor Win64 Development “C:\Users\mrcso\Desktop\Marco[IMPORTANTE]–Studio–\UnrealEngine\ProveQuickProgramming\FPSProject\FPSProject.uproject” -waitmutex” con codice 5. Verificare di disporre di diritti sufficienti per eseguire il comando. FPSProject C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.MakeFile.Targets
Add a first argument UPrimitiveComponent* in your OnHit function
In .h
UFUNCTION()
void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit);
In .cpp:
void AFPSProjectile::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
if (OtherActor != this && OtherComponent->IsSimulatingPhysics())
{
OtherComponent->AddImpulseAtLocation(ProjectileMovementComponent->Velocity * 100.0f, Hit.ImpactPoint);
}
}