[UE4] WITH_EDITOR vs WITH_EDITORONLY_DATA

今天遇到一个问题。

有几个debug用的字段,想要让它在Editor下才起作用,避免游戏运行时的影响性能。

所以在字段定义的地方和使用到它的函数那里分别加了#if WITH_EDITOR宏,就像这样:

// File: MyActor.h
class AMyActor : public AActor
{

public:
#if WITH_EDITOR
    UPROPERTY()
    FString ActorName;
#endif
    void UpdateActorName();
}

// File: MyActor.cpp
void AMyActor::UpdateActorName()
{
#if WITH_EDITOR
    ActorName = this->GetName();
#endif
}

在自己的机器上运行良好,但是打包的时候报错了,提示

ActorName不是AMyActor的成员

报错发生于MyActor.gen.cpp中,这说明UHT并不能识别到头文件中的WITH_EDITOR宏。

遂Google之,查的一文解释甚好。参见此文。

曰:

WITH_EDITORONLY_DATA in headers for wrapping reflected members.

WITH_EDITOR in CPP files for code.. Has nothing to do with reflection.

意思就是:

WITH_EDITORONLY_DATA:头文件中使用包装反射的成员。也就是说UPROPERTY和UFUNCTION包裹的成员变量。在头文件中应该使用。

WITH_EDITOR:在CPP文件的代码中使用,与反射无关,一般只是在CPP中使用的。

你可能感兴趣的:(UE4,C++,ue4)