我正在创建一个“目标小部件”,基本上我希望它执行以下操作:如果玩家正在定位,则在最近的演员上放置一个小部件。它不可能是它自己。
我创建了一个小部件组件来创建小部件,因此我可以在小部件组件内部调用小部件并从那里更新它。例如我有:
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());
}
如您所见,它调用小部件并在所有者上显示目标。但仅在小部件的开头。如何更新此小部件或小部件组件,以便每当我按下目标按钮(选项卡)时,它都会更新我的小部件可见性/所有者?