I have a parent class called: ParentEnemy. From which I have derived all the different types of enemies in my game, for example, ParentEnemy_Alien1.
Here is the Parent Class (ParentEnemy)
// Fill out your copyright notice in the Description page of Project Settings.
#include "ParentEnemy.h"
// Sets default values
AParentEnemy::AParentEnemy()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("CharacterMesh1P"));
Mesh1P->SetOnlyOwnerSee(false);
Mesh1P->SetupAttachment(RootComponent);
Mesh1P->bCastDynamicShadow = false;
Mesh1P->CastShadow = false;
Mesh1P->SetRelativeRotation(FRotator(1.9f, -19.19f, 5.2f));
Mesh1P->SetRelativeLocation(FVector(-0.5f, -4.4f, -155.7f));
}
// Called when the game starts or when spawned
void AParentEnemy::BeginPlay()
{
Super::BeginPlay();
Health = 100;
}
// Called every frame
void AParentEnemy::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AParentEnemy::SetupPlayerInputComponent(UInputComponent *PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void AParentEnemy::DamageTarget(float Damage)
{
Health -= Damage;
if (Health <= 0)
{
DestroyTarget();
}
}
void AParentEnemy::DestroyTarget()
{
Destroy();
}
And here is the child class (ParentEnemy_Alien1) which overrides two methods in the parent class:
// Fill out your copyright notice in the Description page of Project Settings.
#include "ParentEnemy_Alien1.h"
// Called when the game starts or when spawned
void AParentEnemy_Alien1::BeginPlay()
{
Super::BeginPlay();
Health = 100;
}
void AParentEnemy_Alien1::DestroyTarget()
{
Destroy();
UE_LOG(LogTemp, Warning, TEXT("dead"));
}
void AParentEnemy_Alien1::DamageTarget(float Damage)
{
Health -= Damage;
UE_LOG(LogTemp, Warning, TEXT("yo"));
if (Health <= 0)
{
DestroyTarget();
}
}
Now in my projectile class, I have a method that is triggered everytime my projectile hits something:
void AProjectile::OnHit(UPrimitiveComponent *HitComp, AActor *OtherActor, UPrimitiveComponent *OtherComp, FVector NormalImpulse, const FHitResult &Hit)
{
// Only add impulse and destroy projectile if we hit a physics
if ((OtherActor != NULL) && (OtherActor != this))
{
AParentEnemy*Enemy =Cast <AParentEnemy>(OtherActor);
if (Enemy != NULL)
{
Enemy->DamageTarget(50.f);
}
Destroy();
}
}
OtherActor is the actor which my project hit, the ParentEnemy_Alien1. How do I call the override methods by casting the otheractor to the parent class because there can be multiple enemies, therefore multiple child classes that can be hit by my projectile.