Creating a simple Slate

I’m trying to make a slate that only contains an image fullscreen and a text in the middle with a font I added (I haven’t gotten to this part yet)

This is what I tried, but when I’m adding that slate on screen the image doesn’t show, but if I switch to color, then the image is red (with those squares):

void SLoadingScreen::Construct(const FArguments& InArgs) {
	UTexture2D* loadingTexture = LoadObject<UTexture2D>(nullptr, TEXT("Texture'/Game/Materials/Images/LoadingT'"));
	if (IsValid(loadingTexture)) {
		FSlateBrush brush;
		brush.SetResourceObject(loadingTexture);
		brush.ImageSize = FVector2D(800, 800);

		ChildSlot[
			SNew(SOverlay)
				+ SOverlay::Slot()
				.HAlign(HAlign_Fill)
				.VAlign(VAlign_Fill)
				[
					SNew(SImage)
						.Image(&brush)
				]
		];
	}
	else {
		ChildSlot[
			SNew(SOverlay)
				+ SOverlay::Slot()
				.HAlign(HAlign_Fill)
				.VAlign(VAlign_Fill)
				[
					SNew(SImage)
						.ColorAndOpacity(FLinearColor(1, 0, 0, 0.5))
				]
		];
	}
}

What am I doing wrong and the image doesn’t show? I normally use Blueprint, but this time I need to use C++ with Slate.

//Edit:
I also tried:

TSharedPtr<FSlateDynamicImageBrush> loadingTexture = MakeShareable(new FSlateDynamicImageBrush(TEXT("/Game/Materials/Images/LoadingT"), FVector2D(1920.0f, 1080.0f)));

.Image(loadingTexture.Get())

But the same, the image doesn’t show.

Store FSlateBrush as a class member.
Currently, you create it in the function as a local variable, which means that it is destroyed when function exits.

1 Like

I just did a test where I moved both UTexture2D and FSlateBrush inside SLoadingScreen.h with UPROPERTY() as I read somewhere that they might indeed be garbage collected, and yep that was the problem and was about to post the solution to the problem here too, but you beat me to it. :grinning:

Thank you anyway, if it would have taken me 2 more minutes in my testing I would have seen your suggestion, which would have worked.

How that I’m finished with the Image, I moved to the Text part, and I tried this:

				+ SOverlay::Slot()
				.HAlign(HAlign_Center)
				.VAlign(VAlign_Center)
				[
					SNew(STextBlock)
						.Text(FText::FromString(TEXT("Your Text Here")))
						.Font(FSlateFontInfo(FPaths::EngineContentDir() / TEXT("/Game/Fonts/PT_Sherif/PT-Serif_Bold"), 80))
						.ShadowOffset(FVector2D(1, 1)) 
						.ShadowColorAndOpacity(FLinearColor::Black)
						.ColorAndOpacity(FLinearColor(1.0f, 1.0f, 1.0f, 1.0f))
				]

But it doesn’t recognizes the font and I get this:

image

What do I need to change? Also, PT-Serif_Bold is Font Face type, if that matters, and maybe it does.

I do have a font which contains a few families if that is what is needed, but I want to use the Bold version:

I fixed the problem by adding the font in .ttf format, and using:
.Font(FSlateFontInfo(FPaths::ProjectContentDir() / TEXT("Fonts/PT_Serif/PT-Serif_Bold.ttf"), 80))

This was the solution for setting a texture to image and it not being destroyed.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.