0
\$\begingroup\$

I have created an actor cpp class called gun base. for some reason it is causing a perpetual crash in unreal.

this is its header file.

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "GunBase.generated.h"

UCLASS()
class GIZMOSHOOTER_API AGunBase : public AActor
{
    GENERATED_BODY()
    
public: 
    // Sets default values for this actor's properties
    AGunBase();

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

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

private:

    UPROPERTY(EditDefaultsOnly)
    USoundBase* FireSound;
    UPROPERTY(EditDefaultsOnly)
    UParticleSystem* MuzzleFlash;

    UPROPERTY(EditDefaultsOnly)
    USceneComponent* RootSceneComponent = nullptr;
    UPROPERTY(EditDefaultsOnly)
    UStaticMeshComponent* GunMesh = nullptr;
    UPROPERTY(EditDefaultsOnly)
    USceneComponent* BulletSpawnPoint = nullptr;
};

and this is the cpp file.

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


#include "GunBase.h"
#include <Components/StaticMeshComponent.h>
#include <Components/SceneComponent.h>

// Sets default values
AGunBase::AGunBase()
{
    // 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;
    RootSceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
    RootSceneComponent->SetupAttachment(RootComponent);
    GunMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
    GunMesh->SetupAttachment(RootSceneComponent);
    BulletSpawnPoint->SetupAttachment(GunMesh);  //This is the line its complaining about
}

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

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

}

The crash is saying something about an exception access violation, and in particular is referencing line seventeen on the cpp file which has a comment on it. Just in case this means something, I have tried several things to get this code to work, I then builded it, and tried to open the project in unreal again but i always got a crash. thanks.

Edit: I had a function in the header file (pulltrigger its commented out) that was causing me errors but I commented it out and the crash is still occuring.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

The only code that modifies BulletSpawnPoint sets it to nullptr, so dereferencing it will crash. You need to initialize it to something, and/or avoid dereferencing it when it is nullptr.

\$\endgroup\$
1
  • \$\begingroup\$ Oooh, I can't believe I missed that. Thanks a lot, it's working now. I must be more careful. \$\endgroup\$
    – Millard
    Commented Feb 18, 2021 at 0:04
0
\$\begingroup\$

For clarification in case anyone has the same problem in the future, OP didn't create the default sub object for the BulletSpawnPoint so the crash was caused when the editor tried to access it.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .