Amazing User
Amazing User

Reputation: 3563

UE4 crashes because of the simple rotation script

I've made a simple script just like in the video tutorial. It compiles without errors and when I press Play button, the engine crashes. Why is this happening?

enter image description here

.h

#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "MyActor.generated.h"

UCLASS()
class ROTATION_API AMyActor : public AActor
{
    GENERATED_BODY()

public: 
    AMyActor();

protected:
    virtual void BeginPlay() override;

public: 
    virtual void Tick(float DeltaTime) override;
};

.cpp

#include "MyActor.h"

AMyActor::AMyActor()
{
    PrimaryActorTick.bCanEverTick = true;

}

void AMyActor::BeginPlay()
{
    Super::BeginPlay();

    FString a = GetOwner()->GetName(); // ERROR
}

void AMyActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}

Upvotes: 1

Views: 442

Answers (1)

Drake Main
Drake Main

Reputation: 560

To prevent the crash, you should be checking for a nullptr.

auto Owner = this->GetOwner();
if (Owner)
{
  //use Owner
}

That will at least let you print some logs to trace the issue. The issue itself is likely because your AActor derived class has no owner (as in it is not a component of another class). If you are trying to get the name of this AActor, you can just call this->GetName().

Upvotes: 2

Related Questions