# How to keep widgets on screen after changing levels

**URL:** <https://forums.unrealengine.com/t/how-to-keep-widgets-on-screen-after-changing-levels/2351655>\
**Category:** Blueprint\
**Tags:** unreal-engine, Blueprint, CPP, question\
**Created:** [February 23, 2025, 6:37am UTC](https://forums.unrealengine.com/t/how-to-keep-widgets-on-screen-after-changing-levels/2351655 "2025-02-23T06:37:57Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![Valentinvis](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/valentinvis/32/1008648_2.png) [@Valentinvis](https://forums.unrealengine.com/u/Valentinvis)\
**Post date:** [February 23, 2025, 11:11am UTC](https://forums.unrealengine.com/t/how-to-keep-widgets-on-screen-after-changing-levels/2351655/8 "2025-02-23T11:11:23Z")

</div>

For projects using `Blueprints only`, you need to create a function (preferably in a `Blueprint Function Library` so it can be accessed from anywhere), and then call that function after you add the widget to viewport:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/4/b/94b6b06e2f47255401f5e2bd78bd7a318bd02559.jpeg)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/e/b/7eb0d0e60de04a751fc341d8cf662d7f6e878430.png)

The input for the function is of type `Widget`.

If you want, you can also make a macro for it:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/8/1/a81fd20ddfb3b788be774a4610ff9ed54feaddf7.jpeg)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/b/f/3bf3207edf987e25baa018ec152fc27966505d6d.png)

Now for projects that are hybrid, meaning you don’t mind adding some C++ functions for your `Blueprint` project you can add in an existing or new `Blueprint Function Library` the following:

In the `.h` file of the `Blueprint Function Library`:

```auto
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Blueprint/GameViewportSubsystem.h" //You need to add this
#include "Library_Name.generated.h"

/**
 *
 */
UCLASS()
class YOUR_GAME_API ULibrary_Name : public UBlueprintFunctionLibrary
{
	UFUNCTION(BlueprintCallable, Category = "Your_Category_Name")
	static bool KeepWidget(UWidget* widget);
};

```

And now in `.cpp` file of the `Blueprint Function Library`:

```auto
#include "Library_Name.h"

bool ULibrary_Name::KeepWidget(UWidget* Widget)
{
	if (Widget)
	{
		if (Widget->GetWorld())
		{
			if (UGameViewportSubsystem* Subsystem = UGameViewportSubsystem::Get(Widget->GetWorld()))
			{
				FGameViewportWidgetSlot ViewportSlot = Subsystem->GetWidgetSlot(Widget);
				ViewportSlot.bAutoRemoveOnWorldRemoved = false;
				Subsystem->SetWidgetSlot(Widget, ViewportSlot);
				return true;
			}
		}
	}
	return false;
}

```

You can make this function `void` if you want, or any other type for your check in case of a problem. I choose `bool`, if it returns `true` then everything is fine and the widget will remain even after changing levels, but if it returns `false` there was a problem. For a more complete function with checks you can add for each `if else` block a different returned value so that you know what was the problem.

---

_[View the full topic](https://forums.unrealengine.com/t/how-to-keep-widgets-on-screen-after-changing-levels/2351655)._
