【问题标题】:Find components attached to UBlueprint查找附加到 UBlueprint 的组件
【发布时间】:2017-08-01 01:08:41
【问题描述】:

我只想找到附加到蓝图的组件..

现在,我有一个 UProperty,它公开了要设置的蓝图..

UPROPERTY(EditAnywhere, BluePrintReadWrite, Category = "Associated Character")
UBlueprint*                                 BMoveBlueprint;

然后我收到一个设置此属性的事件..

void UTaskComponent::PostEditChangeChainProperty(FPropertyChangedChainEvent& PropertyChangedEvent) {


    Super::PostEditChangeProperty(PropertyChangedEvent);

    //Get the name of the property that was changed  
    FName PropertyName = (PropertyChangedEvent.Property != nullptr) ? PropertyChangedEvent.Property->GetFName() : NAME_None;

    UE_LOG(LogTemp, Warning, TEXT("Property changed: %s"), *PropertyName.ToString());

    if (PropertyName == GET_MEMBER_NAME_CHECKED(UTaskComponent, BMoveBlueprint)) {

        UBMoveComponent *BMoveComponent = (UBMoveComponent *) BMoveBlueprint->FindTemplateByName("BMove");   

    }
}

我只想找到附在蓝图上的组件。这似乎不起作用。

请指教。

【问题讨论】:

    标签: unreal-engine4 unreal-blueprint


    【解决方案1】:

    你可以使用FindComponentByClassUE Docs):

    if (PropertyName == GET_MEMBER_NAME_CHECKED(UTaskComponent, BMoveBlueprint)) 
    {
        auto comp = Cast<UBMoveComponent*>(BMoveBlueprint->FindComponentByClass(UBMoveComponent::StaticClass());   
        if(comp)
        {
            //TODO (we have valid component, do something)
        }
    }
    

    即使NULL 输入也可以使用Cast&lt;&gt;,所以这样做是安全的。

    如果你有多个组件的实例,你可以使用以下:

     TArray<UBMoveComponent*> comps;
    
     GetComponents(comps);
     if(comps.Num() > 0)
     {
         UBMoveComponent* FoundComp = comps[0];
            //do stuff with FoundComp
     }
    

    (代码基于 Rama 的代码UE Answers)。

    【讨论】:

    • UBlueprint 没有 FindComponentByClass 方法。 =[。我正在尝试找到类似的东西......但我想知道这是否是一件明智的事情,因为蓝图本身没有被编译并且没有实例化的成员(?)我真正感兴趣的是得到被拖到现场的蓝图。我认为在这种情况下,蓝图是由它的所有成员编制而成的,如果我能掌握它,那将是最好的选择。
    • 有什么具体原因,为什么AActor* BMoveBlueprint;对你来说不够好?因为如果您只想获取对组件的引用,则不需要UBlueprint。此外我认为,您可以稍后将 blueprint-based 演员投射到UBlueprint*
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多