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

问题描述 投票:0回答:1

我想做的是

在Actor类的运行时创建一个USceneComponent。

    USceneComponent* UnitDesign = NewObject<USceneComponent>(this, FName(*ID));

什么是有效的。

界定 UnitDesign 在头文件(.h)工作没有问题。

UPROPERTY(VisibleAnywhere)  //I can see UnitDesign in World Outliner
USceneComponent* UnitDesign = NewObject<USceneComponent>(this, FName(*ID));

对我来说,不工作的地方是

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

UPROPERTY(VisibleAnywhere)  //I can NOT see UnitDesign in World Outliner
USceneComponent* UnitDesign = NewObject<USceneComponent>(this, FName(*ID));

有什么指点,谢谢。

c++ unreal-engine4
1个回答
3
投票

如果您总是想创建UnitDesign SceneComponent,那么您可以使用 CreateDefaultSubobject (只适用于c'tor) 。

/* .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'tor之后创建一个组件,但你需要更注意什么时候让它和 VisibleAnywhere. 这方面的代码是

UnitDesign = NewObject<USceneComponent>(this, FName(*ID));
UnitDesign->RegisterComponent();

刷新属性面板的偷偷摸摸的方法是调用.NET技术。

GEditor->SelectActor(this, true, true);
GEditor->SelectNone(true, true);

正确的方法是调用 SDetailsView::ForceRefreshIDetailCustomization. 公平警告,这是一个巨大的屁股。

侧面说明 UPROPERTY() 只能用于 .h 文件,因为它需要对虚幻头工具可见。

© www.soinside.com 2019 - 2024. All rights reserved.