Hey Guys,
First of all, really sorry for my bad english
I hope you can help me out. I am really new to c++ / UE an I am following a general c++ tutorial.
My problem is applying the things to unreal engine. I am struggling at calling a basic function from another class.
So I opened the standard FirstPerson Project from ue and with the help of a tutorial i created a raycast line which returns the name of the hitted object.
What I am trying to do now, is creating an Actor “EnemyActor” which has a private Variable ‘int health’ and a public function which is called ‘void ApplyDamage()’.
Whenever I use the Raycast hotkey, the Character class should call the function AEnemyActor::ApplyDamage().
Here is my Code:
EnemyActor.cpp
#include "MyRaycastProject.h"
#include "EnemyActor.h"
#include "Engine.h"
// Sets default values
AEnemyActor::AEnemyActor()
{
// 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;
health = 100;
}
// Called when the game starts or when spawned
void AEnemyActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AEnemyActor::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void AEnemyActor::ApplyDamage()
{
health -= 10;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Applied Damage.. %d Health left"), health));
}
From the MyRaycastProjectCharacter.cpp…
void AMyRaycastProjectCharacter::PerformRaycast()
{
AEnemyActor EActor;
FHitResult* HitResult = new FHitResult;
FVector StartTrace = FirstPersonCameraComponent->GetComponentLocation();
FVector ForwardVector = FirstPersonCameraComponent->GetForwardVector();
FVector EndTrace = ((ForwardVector * 5000.0f) + StartTrace);
FCollisionQueryParams* TraceParams = new FCollisionQueryParams;
if (GetWorld()->LineTraceSingleByChannel(*HitResult, StartTrace, EndTrace, ECC_Visibility, *TraceParams))
{
DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor(255, 0, 0), true);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("You Hit: %s"), *HitResult->Actor->GetName()));
EActor.ApplyDamage();
}
}
The Raycast thing is alright. But whenever I try to call the function ApplyDamage(), my UE just closes and restarts.
I hope you guys can help me. I am facing this problem since 2-3 days. but there are no c++ tutorials for ue.
All I can see is Blueprint ;(