【发布时间】:2021-06-05 02:25:56
【问题描述】:
以下代码一直困扰着我:
ARollingBall::ARollingBall()
{
UStaticMeshComponent* sphere;
sphere = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ball"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Engine/BasicShapes/Sphere"));
sphere->SetupAttachment(RootComponent);
if (SphereVisualAsset.Succeeded()) {
sphere->SetStaticMesh(SphereVisualAsset.Object);
}
}
也就是说,我正在硬编码一条路径。如果我决定我想在路上使用不同的对象怎么办?想象一下,我有多个棋子都硬编码来使用这个资产。在 cpp 中,没有简单的方法可以更改所有这些引用。我有一个好主意,将我的网格选择暴露给蓝图,以利用 Unreal 的参考处理,如下所示:
UPROPERTY(EditDefaultsOnly, Category = "References")
UStaticMesh * m_staticMesh;
但是,当我使用蓝图从此类继承并设置默认网格 (m_staticMesh) 时,以下内容不会创建网格:
ARollingBall::ARollingBall()
{
UStaticMeshComponent* sphere;
sphere = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ball"));
sphere->SetupAttachment(RootComponent);
if (m_staticMesh) {
sphere->SetStaticMesh(m_staticMesh);
}
}
我正在使用 Unreal 4.21.2 两种方法都可以编译,但后者无法正常工作。
对于如何正确实现上述功能的任何建议表示赞赏。如果您知道避免硬编码路径的更好方法,请告诉我。
【问题讨论】:
-
一些测试表明
if(m_staticMesh)条件未能通过。我不明白为什么我已经在我的蓝图子类中设置了默认值。
标签: c++ unreal-engine4 unreal-blueprint