Download

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

Hello,

I’m following a single player tutorial video, when I try to add a widget to the viewport using BeginPlay in my player controller I get “CreateWidget cannot be used on Player Controller with no attached player.” Error.

This is a brand new project in 4.25, this is all that’s created. I’ve tried adding references to self based on what I found online, but I’m not seeing anyone with similar issues in blueprint. I did confirm that this class is inheriting from PlayerController in class settings. What am I doing wrong here?

Sounds like UE4 is complaining about creating the widget because A. the Player Controller isn’t linked to a Pawn / Character… B. Creating a widget isn’t allowed inside a Construction-Script… C. Is it allowed, but because the Construction-Script runs early, code to create the Pawn and posses the Player Controller hasn’t executed yet. D. The BP Possess node for the Pawn / Character to take over the PC doesn’t even exist. These kinds of errors can occur when events happen out of order, key code is delayed, or when you’re not allowed to perform a certain action inside a certain class (or a part of a class: Construction-Script / Function etc).

So… Inserting a delay before creation of the widget should help test this out for now. However, you may not be able to add a delay inside a Construction-Script iirc. So instead, I would move all the widget creation code out of the Construction-Script (it doesn’t need to be there anyway most likely), and place it inside the Event Graph directly, and execute it from Begin-Play (after inserting a Delay;)). BTW1: Construction-Scripts come with lots of gotchas, especially startup-order catches, so try to avoid them whenever possible.

You can also use GetPlayerPawn with IsValid to test if the Pawn / Character exists yet, or F9 breakpoint the Possess node (if one exists) and see where its getting called versus the widget. But again as regards gotchas, you may not be able to actually debug or F9 breakpoint the Construction-Script at all iirc. BTW2: Go easy on Tutorials, they don’t really empower you. Instead try to experiment and learn to solve issues yourself by taking projects apart / dissecting things.

Hey,

Thank you for your reply!

A. I can’t find anywhere how you would link a player controller to a pawn via settings, I have a custom gamemode with everything including the pawn defined in there.

B. I think you may be right, construction script is probably better for getting values like screen resolution before creating UI,

C. Based on what I found searching, I do think your right about this. The tutorial I was watching uses an older version of the UE, there may have been framework changes to the order of things.

D. Thank you for this point, this is a great tip and I’ve taken this down in my notes.

Your right about delay, main graph only, no construct script or functions.

BTW1: Thank you again, a great tip, I think I had a little different of an idea of the construct script’s use, I understand now.

You can breakpoint the construct script, and it looks the error happens right as Create Main Menu Widget fires, this is probably because, the startup order, like you said.

Moving everything to BeginPlay fixes it immediately, even without a delay. Although this really was bugging me as to why this wan’t working, I really wanted to understand why, but honestly your BTW2 is great advice that I should have known to follow in the first place. It was late at night and I wanted to understand the framework better, but again, you are right about tutorials, I wanted to know how you would do procedural logic, from a rougelike dungeon to, something like a SpyHunter road system. Your right, I should learn to take existing projects apart, and experimenting on my own instead of blindly following tutorials, it’s definitely hard to get started with learning to program from a concept.

Thank you for your reply and advice, Cheers!