Not mine, but they helped me a ton and I would hate to have them go to waste. All credit goes to Wraiyth for these.
Start off by creating two files in your Private directory. Slate widgets are generally prefixed with S, so we’ll call is SMyGameSlateHUDWidget. Create a header and cpp file with that name (SMyGameSlateHUDWidget.cpp and SMyGameSlateHUDWidget.h)
Header code:
#pragma once
#include "Slate.h"
class SMyGameSlateHUDWidget : public SCompoundWidget
{
SLATE_BEGIN_ARGS(SMyGameSlateHUDWidget)
: _OwnerHUD()
{}
SLATE_ARGUMENT(TWeakObjectPtr<AShooterHUD>, OwnerHUD)
SLATE_END_ARGS()
/** Needed for every widget */
void Construct(const FArguments& InArgs);
/** returns a string of information to display */
FString GetSomeString() const;
private:
/** Pointer to our parent HUD */
TWeakObjectPtr<class AShooterHUD> OwnerHUD;
};
\CODE]
There's a few important parts here:
Inheriting from SCompoundWidget allows us to create our own Widget that contains other multiple widgets.
All Slate widgets need to include arguments sections. This is where you can initialize properties for this widget through Slate's syntax.
Every Slate widget contains a Construction function called Construct.
CPP file:
#include “ShooterGame.h”
#include “SMyGameSlateHUDWidget.h”
void SMyGameSlateHUDWidget::Construct(const FArguments& InArgs)
{
OwnerHUD = InArgs._OwnerHUD;
ChildSlot
.VAlign(VAlign_Fill)
.HAlign(HAlign_Fill)
SNew(SOverlay)
+SOverlay::Slot()
.VAlign(VAlign_Top)
.HAlign(HAlign_Center)
SNew(STextBlock)
.ShadowColorAndOpacity(FLinearColor::Black)
.ColorAndOpacity(FLinearColor::White)
.ShadowOffset(FIntPoint(-1,1))
.Font(FSlateStyle::GetFontStyle("NormalFont"))
.Text(this, &SMyGameSlateHUDWidget::GetSomeString)
]
];
}
FString SMyGameSlateHUDWidget::GetSomeString() const
{
return FString(TEXT(“Hello!”));
}\Code]
This part is pretty straightforward! We add a new SOverlay to this widget and inside that overlay we create an STextBlock with a variety of properties. Note the bind to GetSomeString for the text for the Widget. If we wanted that text to be conditional or changing (say a Round Timer) then we can perform functionality inside the GetSomeString function to return the text.
SNew will create a new Widget, and then all prpoerties of that widget are accessed and set afterwards.
Thats the entire widget itself! The next step is to actually add it in-game.
We do this inside the HUD.
Inside your HUD’s header file, add a TSharedPtr of your Widget class:
/** HUD menu widget */
TSharedPtr<class SMyGameSlateHUDWidget> MyHUDMenuWidget;
Finally, in DrawHUD(), we need to conditionally construct the Widget and add it to the Viewport
Code:
if ( !MyHUDMenuWidget.IsValid() )
{
SAssignNew(MyHUDMenuWidget,SMyGameSlateHUDWidget)
.OwnerHUD(this);
if ( MyHUDMenuWidget.IsValid() )
{
GEngine->GameViewport->AddViewportWidgetContent(
SNew(SWeakWidget)
.PossiblyNullContent(MyHUDMenuWidget.ToSharedRef())
);
}
}
The important part here is SAssignNew. As opposed to SNew which creates a widget, SAssignNew creates a widget and lets you assign it to a variable for later use. In this case the later use is adding it in AddViewportWidgetContent.
Fire up your game and you should have the word “Hello” printed in the top middle of the screen! Note that Slate doesn’t work in Play-In-Editor - you need to launch the game in a separate window to see the results.
Next tutorial I’ll probably cover Slate Styles and adding some additional controls to a basic HUD widget. Menus will come later
Slate Styles:
Start off by creating two files in your Private directory - MyGameStyle.cpp and MyGameStyle.h
MyGameStyle.h:
#pragma once
#include "Slate.h"
class MyGameStyle
{
public:
/** overrides current slate style */
static void ApplyStyle();
/** reloads textures used by slate renderer */
static void ReloadTextures();
};
MyGameStyle.cpp:
#include "ShooterGame.h"
#include "MyGameStyle.h"
void MyGameStyle::ApplyStyle()
{
FSlateStyle& Style = FSlateStyle::GetInstance();
//Fonts
Style.Set("MyGame.HUDFont", FSlateFontInfo(TEXT("Roboto-Black"), 36));
}
void MyGameStyle::ReloadTextures()
{
FSlateApplication::Get().GetRenderer()->ReloadTextureResources();
}
The important part is the ApplyStyle function. This is where you set your styles for the various Slate controls. In this case we’re setting a Style called MyGame.HUDFont and setting it to use Roboto-Black font with a font size of 36. This function is where you’d add other styles. For instance, you could set a color as such:
Style.Set(“MyGame.SomeFontColor”, FLinearColor(0.6f,1.0f,0.6f));
Or an image like such:
Style.Set(“ShooterGame.MainMenu.BackgroundImage”, new FSlateImageBrush(TEXT(“texture://Game/UI/MainMenu/Background.Background”), ScreenSize));
To use this style, you first need to call ApplyStyle in the context that you are going to use it. In this case, its in the Construct function of our widget.
MyGameStyle::ApplyStyle();
You can then replace the Font in the STextBlock widget to reference your style:
.Font(FSlateStyle::GetFontStyle(“MyGame.HUDFont”))
Done! Fire up the game and your text up the top will be larger.