0

I'm creating a "target widget" which basically I want it to do the following: put a widget on the closest actor if the player is targeting. It cannot be itself.

I created a widget component to create the widget so I can call the widget inside of the widget component and update it from there. For example I have:

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

and inside of my TargetWidget I have :

//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());

}

as you can see, it calls the widget and displays the target on the owner. But only on the start of the widget. How can I update this widget or widget component so whenever I press my target button (tab) it updates my widgets visibility/owner?

mathew
  • 29
  • 5

0 Answers0