1

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

现在,我有一个 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");   

    }
}

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

请指教。

4

1 回答 1

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)
    }
}

Cast<>甚至可以使用NULL输入,所以这样做是安全的。

如果您有多个组件的实例,则可以使用以下内容:

 TArray<UBMoveComponent*> comps;

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

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

于 2017-08-01T11:07:57.223 回答