Crash occurs everytime I hit play

A crash occurs every time I hit play. I even copy pasted the code from the GitHub commits, and added a line according to this post. Here is my code -
ShooterCharacter.cpp :

// Fill out your copyright notice in the Description page of Project Settings.


#include "ShooterCharacter.h"
#include "Gun.h"

// Sets default values
AShooterCharacter::AShooterCharacter()
{
	// 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;

}

// Called when the game starts or when spawned
void AShooterCharacter::BeginPlay()
{
	Super::BeginPlay();

	Health = MaxHealth;

	Gun = GetWorld()->SpawnActor<AGun>(GunClass);
	GetMesh()->HideBoneByName(TEXT("weapon_r"), EPhysBodyOp::PBO_None);
	Gun->AttachToComponent(GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("WeaponSocket"));
	Gun->SetOwner(this);
}

// Called every frame
void AShooterCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void AShooterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &AShooterCharacter::MoveForward);
	PlayerInputComponent->BindAxis(TEXT("LookUp"), this, &APawn::AddControllerPitchInput);
	PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AShooterCharacter::MoveRight);
	PlayerInputComponent->BindAxis(TEXT("LookRight"), this, &APawn::AddControllerYawInput);
	PlayerInputComponent->BindAction(TEXT("Jump"), EInputEvent::IE_Pressed, this, &ACharacter::Jump);
	PlayerInputComponent->BindAction(TEXT("Shoot"), EInputEvent::IE_Pressed, this, &AShooterCharacter::Shoot);
}

float AShooterCharacter::TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser)
{
	float DamageToApply = Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
	DamageToApply = FMath::Min(Health, DamageToApply);
	Health -= DamageToApply;
	UE_LOG(LogTemp, Warning, TEXT("Health left %f"), Health);

	return DamageToApply;
}

void AShooterCharacter::MoveForward(float AxisValue)
{
	AddMovementInput(GetActorForwardVector() * AxisValue);
}

void AShooterCharacter::MoveRight(float AxisValue)
{
	AddMovementInput(GetActorRightVector() * AxisValue);
}


void AShooterCharacter::Shoot()
{
	Gun->PullTrigger();
}

// void AShooterCharacter::LookUp(float AxisValue) 
// {
// 	AddControllerPitchInput(AxisValue);	
// }

ShooterCharacter.h :

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "ShooterCharacter.generated.h"

class AGun;

UCLASS()
class SIMPLESHOOTER_API AShooterCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AShooterCharacter();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	virtual float TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser) override;

private:
	void MoveForward(float AxisValue);
	void MoveRight(float AxisValue);
	void Shoot();

	
	UPROPERTY(EditDefaultsOnly)
		float MaxHealth = 100;

	UPROPERTY(VisibleAnywhere)
		float Health;

	UPROPERTY(EditDefaultsOnly)
		TSubclassOf<AGun> GunClass;

	UPROPERTY()
		AGun* Gun;
};

Gun.cpp:

// Fill out your copyright notice in the Description page of Project Settings.


#include "Gun.h"

#include "Components/SkeletalMeshComponent.h"
#include "Kismet/GameplayStatics.h"
#include "DrawDebugHelpers.h"

// 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"));

	APawn* OwnerPawn = Cast<APawn>(GetOwner());
	if (OwnerPawn == nullptr) return;
	AController* OwnerController = OwnerPawn->GetController();
	if (OwnerController == nullptr) return;

	FVector Location;
	FRotator Rotation;
	OwnerController->GetPlayerViewPoint(Location, Rotation);

	FVector End = Location + Rotation.Vector() * MaxRange;
	// TODO: LineTrace
	FHitResult Hit;
	bool bSuccess = GetWorld()->LineTraceSingleByChannel(Hit, Location, End, ECollisionChannel::ECC_GameTraceChannel1);
	if (bSuccess)
	{
		FVector ShotDirection = -Rotation.Vector();
		UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactEffect, Hit.Location, ShotDirection.Rotation());

		AActor* HitActor = Hit.GetActor();
		if (HitActor != nullptr)
		{
			FPointDamageEvent DamageEvent(Damage, Hit, ShotDirection, nullptr);
			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);

}

Gun.h :

// 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)
		UParticleSystem* ImpactEffect;

	UPROPERTY(EditAnywhere)
		float MaxRange = 1000;

	UPROPERTY(EditAnywhere)
		float Damage = 10;
};

And the crash window -

Hope you are able to solve my problem.
EDIT - Some more info -
I launched the game, and the output log shows this -


Is that a problem?

Did your GunClass get reset to none? That would leave Gun nullptr when you attempt to spawn it.

Sorry, but I didn’t completely understand. Can you reword yourself please?
Also, after launching the game, the output log shows this error -

Fatal error!

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000148

0x00007ffc7e32fc14 UE4Editor-Engine.dll!UnknownFunction []
0x00007ffc64f43762 UE4Editor-SimpleShooter-4732.dll!AShooterCharacter::BeginPlay() [C:\REPOS\SimpleShooter\Source\SimpleShooter\ShooterCharacter.cpp:25]
0x00007ffc7e33c7dd UE4Editor-Engine.dll!UnknownFunction []
0x00007ffc7f63e2e2 UE4Editor-Engine.dll!UnknownFunction []
0x00007ffc7ea4e3b1 UE4Editor-Engine.dll!UnknownFunction []
0x00007ffc7f5e942e UE4Editor-Engine.dll!UnknownFunction []
0x00007ffc7ea6f1d5 UE4Editor-Engine.dll!UnknownFunction []
0x00007ffc7d2d245c UE4Editor-UnrealEd.dll!UnknownFunction []
0x00007ffc7d2f2806 UE4Editor-UnrealEd.dll!UnknownFunction []
0x00007ffc7d30d285 UE4Editor-UnrealEd.dll!UnknownFunction []
0x00007ffc7cdda5f5 UE4Editor-UnrealEd.dll!UnknownFunction []
0x00007ffc7d63e476 UE4Editor-UnrealEd.dll!UnknownFunction []
0x00007ff671327f62 UE4Editor.exe!UnknownFunction []
0x00007ff67133c3ec UE4Editor.exe!UnknownFunction []
0x00007ff67133c4ca UE4Editor.exe!UnknownFunction []
0x00007ff67134ceae UE4Editor.exe!UnknownFunction []
0x00007ff67134fcbe UE4Editor.exe!UnknownFunction []
0x00007ffcd4cf7974 KERNEL32.DLL!UnknownFunction []
0x00007ffcd5b3a271 ntdll.dll!UnknownFunction []

In ShooterCharacter you have this

UPROPERTY(EditDefaultsOnly)
TSubclassOf<AGun> GunClass;

And using it in BeginPlay

Gun = GetWorld()->SpawnActor<AGun>(GunClass);

If GunClass is not set then Gun will fail to spawn and remain nullptr. Which means

Gun->AttachToComponent(GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("WeaponSocket"));
Gun->SetOwner(this);

This code is dereferencing a null pointer.

Yes, that is, many times, the culprit. When I hover over the UPROPERTY GunClass, I get something like this -
image
Is that why I am getting crashes?
Let me get this right, BP_Rifle is the subclass of the C++ class Gun right? I checked that I have a blueprint subclass -
image
Is that what you meant by the GunClass, A.K.A. BP_Rifle being set to none?

No, IntelliSense can’t tell you runtime errors. If that was an issue it would fail to compile.

No, open your BP_ShooterCharcter as that’s where that property exists, you should see it in the details panel.

1 Like

Yes, that was it! I can’t remember myself being so foolish… Thanks, @DanM!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms