Hi all,
Ultra noob with another silly question.
Code was working fine, tried to add an audio component to a base class, compiler threw a heap error, Google told me to go into .target.cs to increase heap memory, did that and the whole thing started crashing with the error in the question subject header. Went back and undid everything, but the error remains. I gather that the code is usually related to hanging pointers in the class constructors so I’m including the header and cpp code below in case anyone can help me spot something egregious:
// Header file.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "PawnBase.generated.h"
class UCapsuleComponent;
class AProjectileBase;
UCLASS()
class TOONTANKS_API APawnBase : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
APawnBase();
private:
// components
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
UCapsuleComponent* CapsuleComp = nullptr;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* BaseMesh = nullptr;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* TurretMesh = nullptr;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
USceneComponent* ProjectileSpawnPoint = nullptr;
// variables
// Limits options to only those in the projectile class i.e. C++ ProjectileClass or BP version
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile Type", meta = (AllowPrivateAccess = "true"))
TSubclassOf<AProjectileBase> ProjectileClass;
protected:
void RotateTurret(FVector LookAtTarget);
void Fire();
virtual void HandleDestruction();
};
```// Cpp
#include "PawnBase.h"
#include "Components/CapsuleComponent.h"
#include "ToonTanks/Actors/ProjectileBase.h"
// Sets default values
APawnBase::APawnBase()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
CapsuleComp = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule Collider"));
RootComponent = CapsuleComp;
BaseMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Base Mesh"));
BaseMesh->SetupAttachment(RootComponent);
TurretMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Turret Mesh"));
TurretMesh->SetupAttachment(BaseMesh);
ProjectileSpawnPoint = CreateDefaultSubobject<USceneComponent>(TEXT("Projectile Spawn"));
ProjectileSpawnPoint->SetupAttachment(TurretMesh);
}
void APawnBase::RotateTurret(FVector LookAtTarget)
{
FVector LookAtTargetCleaned = FVector(LookAtTarget.X, LookAtTarget.Y, TurretMesh->GetComponentLocation().Z);
FVector StartLocation = FVector(TurretMesh->GetComponentLocation());
FRotator TurretRotation = FVector(LookAtTargetCleaned - StartLocation).Rotation();
TurretMesh->SetWorldRotation(TurretRotation);
}
void APawnBase::Fire()
{
if (ProjectileClass)
{
FVector SpawnLocation = ProjectileSpawnPoint->GetComponentLocation();
FRotator SpawnRotation = ProjectileSpawnPoint->GetComponentRotation();
AProjectileBase* TempProjectile = GetWorld()->SpawnActor<AProjectileBase>(ProjectileClass, SpawnLocation, SpawnRotation);
// What does this do?
TempProjectile->SetOwner(this);
};
}
void APawnBase::HandleDestruction()
{
}
Apologies if this is a dumb question but I’m out of ideas and things to Google.
Thanks!