如何在UE4 C ++中更新小部件?

时间:2020-06-21 21:28:56

标签: c++ widget unreal-engine4 unreal-umg

我正在创建一个“目标小部件”,基本上,我希望它执行以下操作:如果玩家正在瞄准,则将小部件放在最近的演员上。它不可能是自己。

我创建了一个小部件组件来创建小部件,因此我可以调用小部件组件内部的小部件并从那里进行更新。例如,我有:

TargetWidgetComponent.cpp

void UTargetWidgetComponent::InitWidget()
{
    Super::InitWidget();
    //if widget isnt null
    if (Widget) {
#if!UE_BUILD_SHIPPING
        //if the widget is not the target widget
        if (!Widget->IsA(UTargetWidget::StaticClass())) {
            //throw this error
            UE_LOG(LogTemp, Warning, TEXT("WidgetClass for %s does not derive from SActorWidget"), *GetNameSafe(GetOwner()));
        }
#endif
        //pointer to call the widget. Casting it so target widget is extended.
        WidgetInst = Cast<UTargetWidget>(Widget);
        //if target widget is not nullptr
        if (WidgetInst) {
                //add the widget to the owner, it cannot be the same actor
            WidgetInst->SetOwningActor(GetOwner());
        }
        
    }

在我的TargetWidget里面:

//TargetWidget.cpp 
void UTargetWidget::SetOwningActor(AActor* NewOwner)
{
    
            //if Owning actor is the same or if the owning pawn is = to the new owner aka itself.
            if (OwningActor == NewOwner || GetOwningPlayerPawn() == NewOwner)
            {
                SetVisibility(ESlateVisibility::Hidden);
                return;
            }
                //if its not, change the owning actor to the new actor
                OwningActor = NewOwner;
                OnOwningActorChanged.Broadcast(NewOwner);
                
                UE_LOG(LogTemp, Warning, TEXT("Owning Actor : %s   , NewOwner: %s"), *OwningActor->GetName(),
                    *NewOwner->GetName());

}
如您所见,

它将调用窗口小部件并在所有者上显示目标。但仅在小部件的开头。如何更新此窗口小部件或窗口小部件组件,以便每当我按下目标按钮(选项卡)时,它都会更新窗口小部件的可见性/所有者?

0 个答案:

没有答案