CreateWidget cannot be used on Player Controller with no attached player

I can’t see a widget during game play. When I press stop button, this error message is shown.

CreateWidget cannot be used on Player Controller with no attached player. FPSPlayerController_0 has no Player attached.

I wanted to show a UMG using C++ code into a Player Controller class. So I made:

  1. a custom Game Mode class.

here is .cpp file’s code.

#include "FPSGameMode.h"
#include "FPSCharacter.h"
#include "FPSPlayerController.h"
#include "UObject/ConstructorHelpers.h"

AFPSGameMode::AFPSGameMode()
{
	// set default pawn class to our Blueprinted character
	static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/Blueprints/BP_Player"));
	DefaultPawnClass = PlayerPawnClassFinder.Class;

	PlayerControllerClass = AFPSPlayerController::StaticClass();

}
  1. a UMG Widget with a simple crosshair image.

  2. a custom Player Controller class

And, I wrote this code below to add the widget to viewport:

#include "FPSPlayerController.h"
#include "Blueprint/UserWidget.h"
#include "UObject/ConstructorHelpers.h"

AFPSPlayerController::AFPSPlayerController()
{
	if (IsLocalPlayerController())
	{
		if (!Widget)
		{
			static ConstructorHelpers::FClassFinder<UUserWidget> UserWidgetFinder(TEXT("/game/UI/CrosshairWidget"));
			Widget = CreateWidget<UUserWidget>(this, UserWidgetFinder.Class);
			
		}
	}
}

void AFPSPlayerController::BeginPlay()
{
	Super::BeginPlay();

	if (Widget)
	{
		Widget->AddToViewport();

		bShowMouseCursor = true;
	}

}

If same question and solution already exist, I’m sorry but I couldn’t find the solution. Please let me know how I can fix it. And if you can, I hope you explain me why this kind of problem happens. Thank you.

The APlayerController instance is just being created, but hasn’t yet been fully initialised - the UObject lifecycle calls things in special ways.

So, try moving your UWidget creation to BeginPlay(), or PostInitializeComponents() - something that occurs after the engine has set up and connected the Player Controller.

Great stuff! Glad I could help :slight_smile:

Thanks for coming back with the update!
Glad you have a solution (in BeginPlay)… with it not working in PostInitializeComponents, I’d wager the Viewport might not be ready/valid at this point, or something similar that the Widget needs.

Thank you for your quick answer. Following your advice, I added a TSubclassOf variable. In the constructor, I made the variable store UserWidgetFinder.Class type. And in the BeginPlay method, I put the widget creation code and it worked.

Thanks.

  • And It didn’t work when I put the same code on the PostInitializeComponents function.

An exception happens that I don’t understand. Can you identify what is wrong?

Usually when you have an access violation where the memory address is 0x0 (with loads more zeroes) it means the object has not been initialized.

Is this object an Event Handler instance?
Or the Sun Actor?

I think you’re correct, it would be an issue to call UMG before BeginPlay.

APlayerController’s, APawn’s, APlayerState’s and several other classes are all torn down and rebuilt on any map transition, including loading a map for the first time (of course, they’re just built if it’s the first time), so they’re not valid for a while.

In my projects, if it’s anything that a designer or artist will be involved with (i.e. displaying any UMG), I’ll invoke a Blueprint Event and fire it up the chain. That way, anything like the creation of UMG will almost certainly be done at the correct time.

You can utilise the BeginPlay node in Blueprints, rather than C++ for such items. You can still house a UWidget* pointer in your C++ code and simply set the returned object from a create widget node (although I think that might return a UUserWidget* instead), and so you have access to it in both C++ and BPs.

No, it’s just a player controller. I think before calling BeginPlay methods for all the actors on the level we can’t call anything about UMG, right?

Not a problem!

And you’re tackling things in a great way to learn! You can totally do everything you’re wanting to do, and deal with things down in C++, it’s just a matter of object life-cycle etc.

Now, as for commercial games using Blueprints - absolutely!
I know a few that were 99% made in BPs and still run at 60fps. I use a hybrid of C++ and BPs, depending on the task. If it needs a lot of maths or processing, then the performance gains of writing it in C++ are there… but when it comes to anything that a designer or artist might touch, I always throw it up to Blueprints. Expose anything that’s needed as Events or variables/properties that we can tweak and adjust without the need to recompile.

I’m watching some online Unreal Engine C++ courses. I can see many instructors using Blueprint classes that inherit C++ classes. Of course, I can feel Blueprint is a beautifully designed system for easy and comfort scripting. But I just wanted to know how to write C++ codes for the same functionalities and how they works while avoid using Blueprint as possible as I can. That’s why I didn’t inherit my C++ classes into the BP children.

Sorry for many questions. But can you let me know that we can use Blueprint not only for prototyping but also for commercial games?

Anyway thank you very much for introducing a good way you use and all the answers above :slight_smile:

I pretty much just use the official API docs, Wiki, Forums and here - or Google when looking for how to implement something specific.

After that, loads of trial and error :stuck_out_tongue:

Good luck!

It’s cool!

Ok, I’ll keep the hybrid way in my mind. And I want to learn how to technically and properly harmonize both C++ and BP as well as digging deeper native codes. For the last, can you introduce any good reference except official API document and Tutorials?