【问题标题】:In my Unreal C++ class, how can I declare a UPROPERTY that will accept only instances of a specific Blueprint class?在我的 Unreal C++ 类中,如何声明只接受特定蓝图类的实例的 UPROPERTY?
【发布时间】:2021-10-29 12:03:27
【问题描述】:

虽然有充分的文档说明如何使用 TSubclassOf<> 声明一个只接受已知 C++ 类的实例的属性,但我的情况是,我希望我的 C++ AActor 派生类包含仅接受对特定蓝图实例的引用的属性。 (该蓝图来自一个不提供 C++ 头文件的包。)

【问题讨论】:

  • 我从来没有做过这种事情,所以这有点远,但我想你可能想用@987654326为“AActor-派生类”写一个details customization @ 并使用 AllowedClassConstructorHelpers::ClassFinder 之类的东西来获取“特定蓝图”的 UClass。
  • 如果您找到适合您的答案,请考虑下面的self-answering,我绝对对如何做到这一点很感兴趣。
  • @ruzihm 我很抱歉。我认为我不应该发布自我答案,因为我没有直接的答案。但间接地,答案可能是:从 C++ 类派生一个蓝图类,然后在蓝图中处理类间通信。如果变得复杂,请在 C++ 类中编写复杂的代码,并使用 Blueprint 中的结果。

标签: c++ unreal-engine4 unreal-blueprint


【解决方案1】:

这是一个有趣的问题,所以我想提供一个可能的答案,这是一种具有最小派生 BP 类和属性反射的解决方法。我在没有可用于编译的引擎的情况下写这个,如果我有任何错误,请道歉。

UCLASS(Blueprintable)
class AMyActor : public AActor
{
    GENERATED_BODY()

public:
    // Name of the required blueprint class property, which must be added in a derived blueprint
    constexpr FName ClassPropertyName = TEXT("BlueprintClass")

protected:
    /* Get the value of the blueprint class property. */
    UClass* GetBlueprintClass();
};


UClass* AMyActor::GetBlueprintClass()
{
    FObjectProperty* BlueprintClassProperty = FindFProperty<FObjectProperty>(GetClass(), ClassPropertyName);
    checkf(BlueprintClassProperty != nullptr, TEXT("AMyActor must used via a derived BP type with a %s property."), *ClassPropertyName.ToString()))
    
    UObject* PropertyValue = BlueprintClassProperty->GetObjectPropertyValue_InContainer(this);
    if (PropertyValue != nullptr)
    {
        return CastChecked<UClass>(PropertyValue);
    }

    return nullptr;
}

您应该能够在 BP 中派生它并添加所需的 BlueprintClass 属性。 BP 端不需要其他逻辑,您只需使用 C++ 中的 GetBlueprintClass 方法获取属性的值。那里有一点开销,所以你可能想要缓存值,例如关于演员构造或 BeginPlay。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 2016-01-01
    • 2018-03-08
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多