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);
}