Hey Guys,
I once posted a question a few months ago, but i can’t edit it somehow i am sorry for that. My editor kept crashing and because I had a bad pc it took me like 20 mins to reopen the project so I decided to take a break with ue4, till I get a new PC.
So my question is that I cannot access to another Class from DefaultPawn.
Here is my Code
The exact question is in PControl.cpp as a comment in my code.
PControl.h (Actor Component of DefaultPawn_BP)
#pragma once
#include "Components/ActorComponent.h"
#include "PControl.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BPPLAYER_API UPControl : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UPControl();
UInputComponent* InputComponent = nullptr;
// Called when the game starts
virtual void BeginPlay() override;
void DoLineTracing();
// Called every frame
virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
void DealEDamage();
};
PControl.cpp
#include "BPPlayer.h"
#include "PControl.h"
#include "EnemyControl.h"
// Sets default values for this component's properties
UPControl::UPControl()
{
// 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.
bWantsBeginPlay = true;
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UPControl::BeginPlay()
{
Super::BeginPlay();
InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
InputComponent->BindAction("Shoot", IE_Pressed, this, &UPControl::DealEDamage);
// ...
}
void UPControl::DoLineTracing()
{
FVector PViewPoint;
FRotator PViewRotation;
FHitResult HitResult;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(PViewPoint, PViewRotation);
GetWorld()->LineTraceSingleByChannel(HitResult, PViewPoint, PViewPoint + (PViewRotation.Vector() * 1000), ECollisionChannel::ECC_Visibility);
AActor* HitActor = HitResult.GetActor();
if (!HitActor)
{
UE_LOG(LogTemp, Warning, TEXT("No HitActor found"));
}
else {
UE_LOG(LogTemp, Warning, TEXT("HitActor found.. Name: %s"), *(HitActor->GetName()));
/*
Here I try to call a function from class EnemyControl
Something like:
HitActor -> setHealth();
setHealth() >> simply sets the health -= 10 and logs out current health
*/
}
}
// Called every frame
void UPControl::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
{
Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
// ...
}
void UPControl::DealEDamage()
{
UE_LOG(LogTemp, Warning, TEXT("Shoot is pressed at Location: %s"), *GetOwner()->GetActorLocation().ToString());
DoLineTracing();
}
EnemyControl.h (Actor Component of an Actor)
#pragma once
#include "Components/ActorComponent.h"
#include "EnemyControl.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BPPLAYER_API UEnemyControl : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UEnemyControl();
// Called when the game starts
virtual void BeginPlay() override;
// Called every frame
virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
void setHealth();
private:
int32 health = 100;
};
EnemyControl.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "BPPlayer.h"
#include "EnemyControl.h"
// Sets default values for this component's properties
UEnemyControl::UEnemyControl()
{
// 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.
bWantsBeginPlay = true;
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UEnemyControl::BeginPlay()
{
Super::BeginPlay();
// ...
}
void UEnemyControl::setHealth()
{
health -= 10;
}
// Called every frame
void UEnemyControl::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
{
Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
// ...
}
I hope I gave you all the information you need and you can help me. I am new to this and am sitting since hours on that.