I want to know if it´s possible to use an editor slate widget inside my game.
I need to make a server list and I was looking for a grid list (like the one in the Scene Outliner panel), and UMG doesn’t have any listbox, or grid listbox.
Yes, Shooter code uses SListView with generated rows for their server list.
Look in: /ShooterGame/ShooterGame/Private/UI/Menu/Widgets/SShooterServerList.cpp
And then you’ll see the following:
//Construct the menu that holds the list of servers
ChildSlot
.VAlign(VAlign_Fill)
.HAlign(HAlign_Fill)
SNew(SVerticalBox)
+SVerticalBox::Slot()
.AutoHeight()
SNew(SBox)
.WidthOverride(600)
.HeightOverride(300)
//Create a new SListView widget for the ServerListWidget
SAssignNew(ServerListWidget, SListView<TSharedPtr<FServerEntry>>)
.ItemHeight(20)
//Use ServerList member variable as the source for this SListView
.ListItemsSource(&ServerList)
//Only allow single selections
.SelectionMode(ESelectionMode::Single)
//Call MakeListViewWidget to generate rows for this ServerListWidget
.OnGenerateRow(this, &SShooterServerList::MakeListViewWidget)
//Call EntrySelectionChanged whenever the user changes the selection
.OnSelectionChanged(this, &SShooterServerList::EntrySelectionChanged)
//Call OnListItemDoubleClicked whenever the user double clicks on an item in the server list
.OnMouseButtonDoubleClick(this,&SShooterServerList::OnListItemDoubleClicked)
//Create a header row showing the Server's name, game mode, number of players, and the ping
.HeaderRow(
SNew(SHeaderRow)
+ SHeaderRow::Column("ServerName").FixedWidth(BoxWidth*2) .DefaultLabel(NSLOCTEXT("ServerList", "ServerNameColumn", "Server Name"))
+ SHeaderRow::Column("GameType") .DefaultLabel(NSLOCTEXT("ServerList", "GameTypeColumn", "Game Type"))
+ SHeaderRow::Column("Map").DefaultLabel(NSLOCTEXT("ServerList", "MapNameColumn", "Map"))
+ SHeaderRow::Column("Players") .DefaultLabel(NSLOCTEXT("ServerList", "PlayersColumn", "Players"))
+ SHeaderRow::Column("Ping") .DefaultLabel(NSLOCTEXT("ServerList", "NetworkPingColumn", "Ping")))
]
]
To add the widget to the viewport look at the SChatWidget or the ScoreboardWidget in ShooterHUD and use the Add/RemoveViewportWidgetContent functions as shown below in ShooterHUD.cpp:
bIsScoreBoardVisible = bEnable;
if (bIsScoreBoardVisible)
{
//Player wants to see the scoreboard, create a new widget and add it to the viewport
SAssignNew(ScoreboardWidgetOverlay,SOverlay)
+SOverlay::Slot()
//Horizontally center it
.HAlign(EHorizontalAlignment::HAlign_Center)
//Vertically center it
.VAlign(EVerticalAlignment::VAlign_Center)
.Padding(FMargin(50))
//Add the ScoreboardWidget to the ScoreboardWidgetOverlay's slot
SAssignNew(ScoreboardWidget, SShooterScoreboardWidget)
//These parameters are required for this widget to
//properly display the scoreboard
.PCOwner(TWeakObjectPtr<APlayerController>(PlayerOwner))
.MatchState(GetMatchState())
];
//Add the ScoreboardWidgetContainer to the GameViewport
GEngine->GameViewport->AddViewportWidgetContent(
SAssignNew(ScoreboardWidgetContainer,SWeakWidget)
.PossiblyNullContent(ScoreboardWidgetOverlay));
if( bFocus )
{
// Give input focus to the scoreboard
FSlateApplication::Get().SetKeyboardFocus(ScoreboardWidget);
}
}
else
{
if (ScoreboardWidgetContainer.IsValid())
{
if (GEngine && GEngine->GameViewport)
{
//Player wants to hide the scoreboard, remove it from the GameViewport
GEngine->GameViewport->RemoveViewportWidgetContent(ScoreboardWidgetContainer.ToSharedRef());
}
}
if( bFocus )
{
// Make sure viewport has focus
FSlateApplication::Get().SetFocusToGameViewport();
}
}
I would recommend creating the slate widget inside a UWidget, that does the initialization noted above. Then you could interact with it from UMG just like any other primitive widget.
I’ve just noticed that a ListView class exists withing UMG source code, in components folder. Any idea if it will be available as a umg widget and when? I don’t think it’s worth to implement that myself if it’s going to be supported by UMG.
Thank you!
Dunno - I marked it experimental until I have time to revisit it. There’s a lot left to wrap which is why I’m recommending a purpose built solution. The issue with the ListView is that it’s a templated C++ class, that concept doesn’t exist in blueprints. So the experimental one in UMG was going to be limited to lists of UObjects only. Right now, users can’t create normal UObjects from blueprints, they need C++ which is why it’s on hold.
If you don’t need sortable header rows or the ability to virtualize a possible set of 10000 entries, then just use a Scrollview with a vertical box in it. Handle the virtualization yourself with pages.