首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >虚幻4.24.3 :如何在.cpp文件中声明UPROPERTY()

虚幻4.24.3 :如何在.cpp文件中声明UPROPERTY()
EN

Stack Overflow用户
提问于 2020-05-28 18:53:58
回答 1查看 128关注 0票数 0

我想做的是:

在运行时在执行元类中创建USceneComponent

代码语言:javascript
复制
    USceneComponent* UnitDesign = NewObject<USceneComponent>(this, FName(*ID));

有效的方法:

在头文件(.h)中定义UnitDesign可以正常工作。

代码语言:javascript
复制
UPROPERTY(VisibleAnywhere)  //I can see UnitDesign in World Outliner
USceneComponent* UnitDesign = NewObject<USceneComponent>(this, FName(*ID));

什么对我不起作用:

在Actor类的BeginPlay()中的CPP文件(.cpp)中定义UnitDesign -

代码语言:javascript
复制
UPROPERTY(VisibleAnywhere)  //I can NOT see UnitDesign in World Outliner
USceneComponent* UnitDesign = NewObject<USceneComponent>(this, FName(*ID));

感谢您的指点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-28 22:04:08

如果您总是想创建UnitDesign SceneComponent,那么您可以使用CreateDefaultSubobject (仅限c‘’tor):

代码语言:javascript
复制
/* .h */
UCLASS()
class AYourActor : public AActor
{
    GENERATED_BODY()

public:
    AYourActor(FObjectInitializer const& ObjectInitializer);

    UPROPERTY(VisibleAnywhere)
    USceneComponent* UnitDesign;
};

/* .cpp */
AYourActor::AYourActor(FObjectInitializer const& ObjectInitializer) :
    UnitDesign(nullptr) /* this is only needed if UnitDesign is not initialized below */
{
    UnitDesign = CreateDefaultSubobject<USceneComponent>(TEXT("Unit Design"));
}

如果这是一个可选的东西,那就有点复杂了,你可以在c‘to之后创建一个组件,但你需要更多地意识到什么时候让它与VisibleAnywhere配合得更好。这样做的代码应该是

代码语言:javascript
复制
UnitDesign = NewObject<USceneComponent>(this, FName(*ID));
UnitDesign->RegisterComponent();

刷新属性面板的秘密方法是调用:

代码语言:javascript
复制
GEditor->SelectActor(this, true, true);
GEditor->SelectNone(true, true);

“正确”的方法是从IDetailCustomization调用SDetailsView::ForceRefresh。警告你,这是个大笨蛋。

旁注:UPROPERTY()只能在.h文件中使用,因为它需要对虚幻头工具可见。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62062968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档