【问题标题】:UE4 crashes because of the simple rotation scriptUE4 因简单的旋转脚本而崩溃
【发布时间】:2018-03-03 07:50:00
【问题描述】:

我已经制作了一个简单的脚本,就像在视频教程中一样。它编译没有错误,当我按下播放按钮时,引擎崩溃。为什么会这样?

.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);
}

【问题讨论】:

  • 完全不了解 Unreal:GetOwner()->GetName() 闻起来像 nullptr。 GetOwner() 在某些情况下是否返回 nullptr
  • @user2328447 我猜是的,但不知道为什么。它不应该为空,它可以在视频教程中使用

标签: c++ unreal-engine4


【解决方案1】:

为防止崩溃,您应该检查 nullptr。

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

这至少可以让您打印一些日志来跟踪问题。问题本身很可能是因为您的 AActor 派生类没有所有者(因为它不是另一个类的组件)。如果你想得到thisAActor 的名字,你可以打电话给this->GetName()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    相关资源
    最近更新 更多