Slate disappears from in game screen?

I have set up a Color Picker using a Slate button which has an image of different colors and when clicked sets a vector parameter I have set up in the Material Editor to change the color of an object.

It’s pretty straight forward and seems to work fine when running the game in the editor however when running in a standalone game I have 2 problems.

  1. The Slate completely disappears from the in game screen after around 60 seconds.

  2. When playing in full screen mode the FPlatformMisc::GetScreenPixelColor function I use passing in the current Mouse Position as parameters the only color that is ever returned is completely black yet in windowed mode or windowed full screen mode the correct colors are always found

//set up the slate button

ChildSlot
.VAlign(VAlign_Top)
.HAlign(HAlign_Left)
[
	SNew(SButton)
	.DesiredSizeScale(FVector2D(0.5, 0.5))
	.OnClicked(this, &STestUIWidget::OnClicked)
	.Visibility(EVisibility::Visible)
	[
		SNew(SImage)
		.Image(ColorPicker)
	]
];

//called when button is clicked

    FReply STestUIWidget::OnClicked()
    {
      CurrentMaterial = SpawnHandler->GetCurrentMaterial();
      CurrentBall = SpawnHandler->GetCurrentBall();

      CurrentController = UGameplayStatics::GetPlayerController(World, 0);
      FVector2D MousePos = GEngine->GameViewport->GetMousePosition();

      BallColor = FPlatformMisc::GetScreenPixelColor(MousePos);

      CurrentMaterial->SetVectorParameterValue(FName("Color"), BallColor);

      return FReply::Handled();
    }

What platform are you running the game on?

I’m running on Windows - PC

The Slate completely disappears from
the in game screen after around 60
seconds.

this most probably mean that your widgets are garbage collected. Where do you keep reference to you widgets and how do you set them up?

I followed one of the few starter tutorial code samples I could find on slates the above code I posted is setup in the widgets class construct I then reference this widget in my HUD class.

void ATestHUD::BeginPlay()
{
	SAssignNew(TestUIWidget, STestUIWidget).OwnerHUD(this);

	if (GEngine->IsValidLowLevel())
	{
		GEngine->GameViewport->AddViewportWidgetContent(SNew(SWeakWidget).PossiblyNullContent(TestUIWidget.ToSharedRef()));
	}

	if (TestUIWidget.IsValid())
	{
		TestUIWidget->SetVisibility(EVisibility::Visible);
	}
}

void ATestHUD::DrawHUD()
{
	Super::DrawHUD();
}

Why you want to add WeakWidget to the viewport? SWeakWidget will be removed from memory if theres no reference to it during garbage collect. So if you really want to use WeakWidget keep a reference to it or just add your TestUIWidget to Viewport.

Edit:
I actually misunderstood concept of SWeakWidget so ignore this comment

I believe SWeakWidget is used there to prevent the engine code holding onto a game code reference when performing a hot-reload:

I would hope that the TestUIWidget pointer (see the SAssignNew) will be keeping the widget alive (assuming that’s a member variable?).

Yes it is member variabled my TestHUD.h looks like this

UCLASS()
class ATestHUD : public AHUD
{
	GENERATED_UCLASS_BODY()

	TSharedPtr<class STestUIWidget > TestUIWidget;

	void BeginPlay();

	void DrawHUD();
	
};

Like I said the knowledge I had of Slates was basically just from the tutorial I followed however I have done more research on them now and well there seems to be a lot more to SWeakWidget than I originally thought.

In terms of just adding the TestUIWidget to the viewport I did try this

GEngine->GameViewport->AddViewportWidgetContent(TestUIWidget.ToSharedRef());

However the result was still the same

Also the widget will always remain there when playing within the editor so I’m wondering if it’s some sort of packaging issue? When playing in a standalone game the widget dissappears after a certain time yet when actually launching the game the widget isn’t there at all.

I access my widgets resources like so.

TSharedRef<FSlateGameResources> SlateResources = FSlateGameResources::New(FName("Style"), "/Game/Styles", "/Game/Styles");

	FSlateGameResources &Style = SlateResources.Get();

	FSlateStyleRegistry::RegisterSlateStyle(Style);

	ColorPicker = Style.GetBrush(FName("ColorWheel_Brush"));

This is probably unrelated to your issue, but I noticed that UGameViewportClient::GetMousePosition is in client space, whereas FPlatformMisc::GetScreenPixelColor expects a screen space point.

This isn’t an issue when running the game in its own window, but will mess up the picking when running in an editor viewport.

You probably want this instead:

FIntPoint MousePos;
GEngine->GameViewport->Viewport->GetMousePos(MousePos, false/*bLocalPosition*/);

BallColor = FPlatformMisc::GetScreenPixelColor(MousePos);

hello,can you leave your email address?May I ask you some question?