Download

Setting up the "detect hit" function in C++

Hey guys,

I’m trying to get it so that my main character can detect when it hits (bumps into) one of my enemy characters in the game. So far I have this code:

Header file:


#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Components/CapsuleComponent.h"
#include "MainCharacterController1.generated.h"

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CPLUSPLUSTEST2_API UMainCharacterController1 : public UActorComponent
{
 GENERATED_BODY()

public: 
 // Sets default values for this component's properties
 UMainCharacterController1();

protected:
 // Called when the game starts
 virtual void BeginPlay() override;

public: 
 // Called every frame
 virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

 UFUNCTION()
 void OnHit1(AActor * OtherActor, UPrimitiveComponent * OtherComp, FVector NormalImpulse, const FHitResult& Hit);

private:
 AActor* mainCharacterActor; // The owning actor of this component script
 UCapsuleComponent* mainCharacterCapsule; // Main character's capsule collider

};


CPP file:


#include "MainCharacterController1.h"
#include "GameFramework/Actor.h"
#include "Components/CapsuleComponent.h"
#include "Components/PrimitiveComponent.h"


// Sets default values for this component's properties
UMainCharacterController1::UMainCharacterController1()
{
 // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
 // off to improve performance if you don't need them.
 PrimaryComponentTick.bCanEverTick = true;


}


// Called when the game starts
void UMainCharacterController1::BeginPlay()
{
 Super::BeginPlay();

 mainCharacterActor = GetOwner();
 mainCharacterCapsule = mainCharacterActor->FindComponentByClass<UCapsuleComponent>();

 //mainCharacterCapsule = ObjectInitializer.CreateDefaultSubobject

 mainCharacterCapsule->OnComponentHit.AddDynamic(this, &UMainCharacterController1::OnHit1);

}


// Called every frame
void UMainCharacterController1::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
 Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

}



//void UMainCharacterController1::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
void UMainCharacterController1::OnHit1(AActor * OtherActor, UPrimitiveComponent * OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
 // Other Actor is the actor that triggered the event. Check that is not ourself. 
 if ((OtherActor != nullptr) && (OtherActor != mainCharacterActor) && (OtherComp != nullptr))
 {
  // Turn off the light 
  // PointLight->SetVisibility(false);
  UE_LOG(LogTemp, Warning, TEXT("I collided with something!"));
 }
}


I’ve seen different versions, some with initializers etc… , but also most of the examples seem to be on a standalone “Actor” object. My scripts are “Actor Component” C++ files that I attached to my main character. I’m not sure if that has anything to do with anything…

Also, the code line below is where I’m getting the error/problem, as it doesn’t seem to be “setup” correctly for the “AddDynamic” function to work. And I’m not sure why or what to do. It’s this code line in the CPP script:


mainCharacterCapsule->OnComponentHit.AddDynamic(this, &UMainCharacterController1::OnHit1);

Can I do this from an Actor Component on my main character? Or do I need to do it from somewhere else?

Thanks for any help!

I solved it. Just posting in case anyone else has a problem. The version of the Unreal API mattered in my case. I had the wrong parameters, because all of the examples I was following were from older API versions apparently, and the function parameters were not correct. Once I found the current/correct OnHit parameters, it worked :slight_smile:

I’m using 4.17 and the hit paramters are:
“OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)”

Thank You, save my hours.