我是UE4开发的新手,并且已经学习了Udemy的虚幻引擎开发类(class)。我在Actor上创建了一个新的Component,名为PositionReporter,标题为PositionReporter.h
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "PositionReporter.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UPositionReporter : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UPositionReporter();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};
和PositionReporter.cpp中的代码被
#include "PositionReporter.h"
// Sets default values for this component's properties
UPositionReporter::UPositionReporter()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UPositionReporter::BeginPlay()
{
Super::BeginPlay();
FString t = GetOwner()->GetActorLabel();
UE_LOG(LogTemp, Warning, TEXT("Position Reporter reporting for duty on %s"), *t);
}
// Called every frame
void UPositionReporter::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
如您所见,我现在尝试在指向通过GetObject()检索的AActor的指针上调用GetName函数。
但是,一旦我键入“GetObject()->”,就不会弹出自动完成功能(就像在视频中一样),并且当我手动添加“GetName()”时,出现编译器错误“指向不完整类类型的指针不是允许”。
怎么了?我是否缺少进口商品?我已经将我的代码与Ben的git repo进行了比较,但是找不到任何区别。我正在使用虚幻编辑器4.16.0!
我注意到了另一件奇怪的事情:当我从Unreal Engine Editor编译所有内容时,它可以编译并运行良好。但是,当我使用VS 2017进行编译时,我得到了错误,并且我也没有得到自动完成功能,这真是一个不小的惊喜。我想念什么?
最佳答案
在PositionReporter.h上包含Engine.h可以解决此问题。
#pragma once
#include "Engine.h" // <- This
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "PositionReporter.generated.h"
您需要花一些时间...实际上,我关闭并重新打开了解决方案,以使其不再显示不存在的错误并提供自动完成功能。
注意:正如其他文章中提到的那样,此解决方案可以很好地实现智能感知自动完成功能,但也不是最好的解决方案,因为它将包含大量内容并大大增加了编译时间。
包括特定的.h文件可能会更好,但是您需要知道要包括的.h文件,而且您可能不知道。
我发现的最佳解决方案是放弃Intellisense,并使用Resharper进行代码自动完成,它可以快速运行,可以正确自动完成,并且您不需要包括任何其他文件。