How to setup replication for c++ UserWidget's textblock?

Hello my fellow enthousiasts,

A Quick overview of what I want to achieve:

In a multiplayer game
APickup Actor has a WidgetComponent.
When the player gets close to the Actor, the Widget’s visibility is turned on and the player can see the TextBlock’s text.
This value could be updated and needs to be able to change.
Only the player that is close to the Pickup can see the text, not all players.

Here a quick overview of what I have setup:

UPickupNameWidget : public UUserWidget

  • Has a UTextBlock* PickupName.

  • Has a function SetPickupName(FText NewPickupName);

  • A WBP_PickupNameWidget is created with correct binding TextBlock PickupName.

if(!HasAuthority()){

		PickupWidget =  CreateWidget<UPickupNameWidget>(GetWorld(), WidgetClass);
		if(PickupWidget){
			PickupWidget->SetPickupName(PickupName); 
			Widget->SetWidget(PickupWidget);
			Widget->SetVisibility(bShowPickupName); 
		}
	}

APickup: public AActor

  • Has a WidgetComponent

    • Widget of type UPickupNameWidget is created in BeginPlay and set to the WidgetComponent.
  • Has SetWidgetPickupName(FText NewPickupName){}

    • Here I currently do the following:
UPickupNameWidget* widget = Cast<UPickupNameWidget>(Widget->GetWidget());
   		widget->SetPickupName(NewPickupName);

My first question, Is this the correct way of doing this?
Or is there a better, more performant way to retrieve the widget and set the TextBlock’s value?
Is a cast for every text update expensive?


Now another question: What do I have to do to actually update this value when the player walks to the object?

With some ulogs & a new function GetPickupName() I can log and see the old vs new value of the textblock’s content and it actually updates it, but it isn’t changed on the screen itself.

I also have a bShowPickupName boolean, that will set the visibility of the Widget on or off using the function SetShowPickupName(bool NewShowPickupName).
When this is called, I change Widget->SetVisibility(NewShowPickupName), but the widgets keeps visible…

The Pickup Actor also keeps a FText variable: PickupName that will also store the PickupName.
This variable is Replicated.
&& The Pickup Actor has a bShowPickupName variable, also replicated.
Using SetShowPickupName(false), I can see in the logs that the function is executed.
(This calls WidgetComponent->SetVisibility(false)). But the widget keeps showing…

I’m kinda lost in the dark here, I’ve seen multiple widget tutorials, but they all seem to just show to bind a textblock and finish there.

Any help is much appreciated!