Accessing widget from another class

You could use GameInstance as a central repository. It is generally available from all parts of the system:

UCLASS()
class UMyGameGameInstance : public UGameInstance
{
    GENERATED_BODY()
    :
};


UMyGameInstance* MyGameInstance = GetWorld()->GetGameInstance<UMyGameInstance>();

if (MyGameInstance)
{
   :
}

I’m working on the code for a game player list which I want to add children to as players are added to the game. Currently I do a call to “MyW= CreateWidget(GetWorld(), wplaylis); MyW->AddToViewport();” when the player joins the game where wplaylis is a reference to the player list widget where the children would be added. Whenever a new player is added in the game, I want to have a call to “AddChild” so that the new player name can be added to wplaylis. How can I make it so that I can access this widget instance from another class and add the playername children to it?

Thanks for the comment. The way I currently have it is:

Player Clicks “Join Button” from starter screen widget and in the OnClicked join button function in the starter screen class, I have a call to “Create player list widget and add to viewport”.

In my current game instance class, I have a function (“OnPlayerAdded”) that detects when players are added to the game. I wanted to add a new player child to the player list widget’s scrollbox everytime this function is called. Though it seems that only way I can do that so far is if I do another call to create play list widget and add child, every time a player is added.I’m wondering how to access the already created widget in the scene.

Can you access widgets that currently exist within the game through game instance?

A quick reply but if I have time later I’ll try and follow up with a cleaner way (binding):

UPROPERTY()
UScrollBox* playerHistory;

const FName PlayerHistoryScrollBoxName = FName(TEXT("PlayerHistoryScrollbox"));

playerHistory = (UScrollBox*)(WidgetTree->FindWidget(PlayerHistoryScrollBoxName));

ok, thanks. In my “PlayerListWidget.h” class which is the parent to the player list blueprint widget, I have

UScrollBox* CurrPlayers;

//addedplayer is reference to player BP widget to add to Currplayers scrollbox
UPROPERTY(BlueprintReadWrite, Category = “Widgets”)
TSubclassOf addedplayer;

In MyStarterMenuWidget.h I defined the following:

UPlayerListWidget* MyW;

//wplaylis assigned to player list widget blueprint( widget with just panel and empty scrollbox)

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Widgets”)
TSubclassOf wplaylis;

In MyStarterMenuWidget.cpp (in Join button function I have):

//adds empty playerlist scroll box to screen

void UMyStarterMenuWidget::JoinGame()
{
if (wplaylis) {
MyplayerList= CreateWidget(GetWorld()->GetFirstPlayerController(), wplaylis);
Myplayerlist->AddToViewport();
}
}

In MyGameInstance.h class I have:
UPROPERTY(BlueprintReadWrite, Category = “Widgets”)
TSubclassOf wPlayer;

UPROPERTY( BlueprintReadWrite, Category = “Widgets”)
TSubclassOf wPlayerList;

In my MyGameInstance.cpp I have

void MyGameInstance::OnPlayerAdded(){

 //wPlayerlist variable assigned to player list widget blueprint
 //wPlayer variable assigned to player widget blueprint

if (wPlayerList && wPlayer) // Check if the Asset is assigned in the blueprint.

{
UPlayerListWidget *playersList = Cast(wPlayerList->GetClass());
player= CreateWidget(GetWorld(), wPlayer);
//add player to Currplayers scrollbox
er->CurrPlayers->AddChild(player);

}

However it throws an error in saying “playersList” in OnPlayerAdded() is NULL. How can I change OnPlayerAdded() so I can make a call to CurrPlayers scrollbox in PlayerListWidget and add “player” child?