OnHit Component Code NOT WORKING C++ ???!!!

I’ve implemented the C++ code in my .cpp file from the UE documentation. The code generates so many errors in the .cpp file. I don’t know why, I’ve been trying to solve this issue for days. Can someone help please?? I’m currently using UE 4.27 and using Visual Studio code.

This is what I have in my .h file:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Cube.generated.h"

UCLASS()
class MODULE_TWO_STONE_API ACube : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ACube();

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

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};


	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
class UStaticMeshComponent* CubeMesh;

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UMaterialInstance* CubeMaterial;

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UMaterialInstance* DamagedCubeMaterial;

FTimerHandle DamageTimer;



	UFUNCTION()

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

And this is my .cpp code:


#include "Cube.h"
#include "Kismet/GameplayStatics.h"
#include "OnHitProjectile.h"

#define PrintString(String) GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::White,String)

// Sets default values
ACube::ACube()
{
	// 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;
	CubeMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CubeMesh"));
	DamagedCubeMaterial = CreateDefaultSubobject<UMaterialInstance>(TEXT("DamageMaterial"));
	CubeMaterial = CreateDefaultSubobject<UMaterialInstance>(TEXT("CubeMaterial"));

	CubeMesh->SetSimulatePhysics(true);

}

// Called when the game starts or when spawned


void ACube::BeginPlay()
{
    {
        Super::BeginPlay();
        CubeMesh->OnComponentHit.AddDynamic(this, &ACube::OnComponentHit);
    }

    void ACube::OnTakeDamage()
    {
        CubeMesh->SetMaterial(0, DamagedCubeMaterial);
        GetWorld()->GetTimerManager().SetTimer(DamageTimer, this, &ACube::ResetDamage, 1.5f, false);
    }



    void ACube::ResetDamage()
    {
        CubeMesh->SetMaterial(0, CubeMaterial);
    }

    void ACube::OnComponentHit(UPrimitiveComponent * HitComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, FVector NormalImpulse, const FHitResult & Hit)
    {

        if (AOnHitProjectile* HitActor = Cast<AOnHitProjectile>(OtherActor))
        {
            UGameplayStatics::ApplyDamage(this, 20.0f, nullptr, OtherActor, UDamageType::StaticClass());
            OnTakeDamage();
        }
    }

    // Called every frame
    void ACube::Tick(float DeltaTime)
    {
        Super::Tick(DeltaTime);

    }

which guide were you trying to follow?

I kind of hope this is a copying mistake, and not a direct rip from your files:
in you Cube.h everything after the }; is not included in the class as that combo of symbols in C++ signify the end of a classes/structs/enums initial definition.
and everything that is specific to that Thing are between the designation opening-Curly-Brace and the Closing-Curly-Brace semi-colon

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Cube.generated.h"

UCLASS()
class MODULE_TWO_STONE_API ACube : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ACube();

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

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
	class UStaticMeshComponent* CubeMesh;

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
	UMaterialInstance* CubeMaterial;

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
	UMaterialInstance* DamagedCubeMaterial;

	FTimerHandle DamageTimer;

	UFUNCTION()

	void OnComponentHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
};

another way to see is in the .cpp file the ACube::OnComponentHit() the function name itself should be underlined in Red in Visual Studio when you hove over it something like

class “ACube” has not member “OnComponentHit”

along with every member variable of ACube should also be underlined in red with Identifier "[VarName]" is undefined
I am not paying attention to what the access specifiers for each of these members added into the class are, but that can come later.

also in the Cube.cpp your braces for BeginPlay() are mis-matched

void ACube::BeginPlay()
// { 'this will cause problems'
    {
        Super::BeginPlay();
        CubeMesh->OnComponentHit.AddDynamic(this, &ACube::OnComponentHit);
    }

    void ACube::OnTakeDamage() // 'this should be underlined in red as well'
    {
        CubeMesh->SetMaterial(0, DamagedCubeMaterial);
        GetWorld()->GetTimerManager().SetTimer(DamageTimer, this, &ACube::ResetDamage, 1.5f, false);
    }

Hi,
I was using this documentation: Using the OnHit Event | Unreal Engine 5.3 Documentation

Apparently, the doc has been updated with less information. I’m using UE 4.27 for my class and the doc is for UE 5. I ended up solving the issue I was having. I included the OnProjectileHit class, the doc never specified when I should’ve added that in…
Thank You for replying

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.