Hi,
It happened again, I was following the video, doing almost everything the same as Sam, but still, I get an error and he doesn’t.
The errors:
My Gun.cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Gun.h"
#include "Components/SkeletalMeshComponent.h"
#include "Kismet/GameplayStatics.h"
#include "GameFramework/Pawn.h"
#include "GameFramework/Actor.h"
#define OUT
// Sets default values
AGun::AGun()
{
// 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;
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
SetRootComponent(Root);
Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
Mesh->SetupAttachment(Root);
}
void AGun::PullTrigger()
{
UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, Mesh, TEXT("MuzzleFlashSocket"));
FHitResult Hit;
FVector ShotDirection;
bool bSucces = GunTrace(Hit, ShotDirection);
if (bSucces) {
if (ImpactEffect == nullptr) return;
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactEffect, Hit.Location, ShotDirection.Rotation());
AActor* HitActor = Hit.GetActor();
if (HitActor != nullptr) {
FPointDamageEvent DamageEvent(Damage, Hit, ShotDirection, nullptr);
AController* OwnerController = GetOwnerController();
HitActor->TakeDamage(Damage, DamageEvent, OwnerController, this);
}
}
}
// Called when the game starts or when spawned
void AGun::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AGun::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
bool GunTrace(FHitResult& Hit, FVector& ShotDirection)
{
AController* OwnerController = GetOwnerController();
if (OwnerController == nullptr) {
return false;
}
FVector OwnerLocation;
FRotator OwnerRotation;
OwnerController->GetPlayerViewPoint(
OUT OwnerLocation,
OUT OwnerRotation
);
ShotDirection = -OwnerRotation.Vector();
FVector End = OwnerLocation + OwnerRotation.Vector() * MaxRange;
FCollisionQueryParams Params;
Params.AddIgnoredActor(this);
Params.AddIgnoredActor(GetOwner());
return GetWorld()->LineTraceSingleByChannel(OUT Hit, OwnerLocation, End, ECollisionChannel::ECC_GameTraceChannel1, Params);
}
AController* AGun::GetOwnerController() const
{
APawn* OwnerPawn = Cast<APawn>(GetOwner());
if (OwnerPawn == nullptr) return nullptr;
return OwnerPawn->GetController();
}
My Gun.h file:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Gun.generated.h"
UCLASS()
class SIMPLESHOOTER_API AGun : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AGun();
void PullTrigger();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
UPROPERTY(VisibleAnywhere)
USceneComponent* Root;
UPROPERTY(VisibleAnywhere)
USkeletalMeshComponent* Mesh;
UPROPERTY(EditAnywhere)
UParticleSystem* MuzzleFlash;
UPROPERTY(EditAnywhere)
float MaxRange = 1000.f;
UPROPERTY(EditAnywhere)
UParticleSystem* ImpactEffect;
UPROPERTY(EditAnywhere)
float Damage = 10.f;
bool GunTrace(FHitResult &Hit, FVector& ShotDirection);
AController* GetOwnerController() const;
};
Sam didn’t show his includes this lesson so I wasn’t able to check if I had the same includes, but after adding “#include “GameFramework/Pawn.h”” and even “#include “GameFramework/Pawn.h”” to make sure it still didn’t work.
It doesn’t even recognize a function in its own header file (GetOwnerController).
I did add the #define OUT and added this in some places but I don’t think that can have such a major impact.
As always, any help is much appreciated and sorry if I missed something stupid.
Matis