Why am I only getting error when event ISN'T triggered but no error when it IS?

Event Destruct that’s raising error:

Error messages:

Event Construct:

Function in Player Controller that triggers Event Construct and Event Destruct:

I’m using Event Destruct with User Widget class. I think it’s saying “Get Player Controller” is returning a null/invalid object. This error message only appears when Event Construct is triggered and I haven’t triggered Event Destruct yet. Once I trigger Event Destruct then the error doesn’t appear. But the error says that Event Destruct is causing the error so I’m confused… How is an event that never occurs creating an error? Why would triggering that event fix the error?

IMC should occur in the Controller class.
UI should be handled by the controller class.

Get Player Controller (0) is a single player node. Get Player Controller (0) when executed on the server always points to Host or first joined client.

If you are in the controller class then use Self.


Event Construct is a Widgets Begin Play node. Controller may not be loaded when the event triggers, thus a Null pointer.

Event Destruct is a standard UE5 event on Widgets that you’re using. It says it’s “Called when a widget is no longer referenced, causing the slate widget to be destroyed.” So likely what is happening is that somewhere you are removing any references to the widget, and UE5 is then calling Event Destruct, which then causes the error.

In the “Toggle Build Menu” code you’re removing the Build Menu from its parent. I’m assuming there’s nothing else referencing the Build Menu, so Event Destruct is likely being called when you call “Toggle Build Menu.”

As for why it’s causing errors, my best guess is that you’re calling this “Toggle Build Menu” from a widget’s Pre-Construct or Construct event, and so your Event Destruct may be getting called and attempting to access the PlayerController before the PlayerController has finished being constructed.

My suggestion is to make a new event or function that is called something other than Event Destruct. Also, if you’re not intending to have your Build Menu widget destroyed when you remove it from its parent then you likely need to go about whatever you’re trying to do there in a different manner. Since UE is calling Event Destruct then it means its trying to destroy that widget.

Thanks for your help. I probably don’t want to destroy the widget so maybe I need to go about this a little differently. Pretty new to this so idk what I’m doing half the time. The thing is when i add “Print Text” nodes everything is working the way i want it to. Build menu is opening/closing and input mapping contexts appear to be switching correctly.

To clarify: BuildMenu widget is initialized/created in PlayerController and saved to variable at BeginPlay. PlayerController then calls “Toggle Build Mode” at press of a button, which tells BuildMenu widget to either Construct or Destruct itself, which im able to do as much as I want using the same widget reference I created at BeginPlay. Destruct isn’t getting called when I get the error (according to “Print Text”), but once I call Destruct, I don’t get the error anymore. So that’s why I’m confused. Why is UE5 telling me that Destruct is causing an error if it’s never being called? Another thing that confused me was I used “isValid?” on PlayerController and it said it was valid

If you select the Event Destruct node and press F9 (to make a breakpoint) you can tell for sure whether it is getting called. You’ll then see some buttons to the right of the Stop Play mode button that you can use to step through the execution of your code. If you hover over the Get Player Controller node’s return value you can see whether it’s null or has a value (it will show Unknown for null and an object name if it is valid).

When you’re checking the return value of a node make sure you’ve executed past when the node would be called. If you try to check the value before the node is called it will always be null

IMC should 100% be managed by the controller, not from a widget. Create a BP interface and add it to the controller. Next create the BPI events you’ll need.

Add BuildAdd, Add BuildEdit
Remove BuildAdd, Remove BuildEdit

Then in the Controller class Implement the interface events. Then apply the proper logic to each.

In the Widgets you can then simply Get Owner → Add/Remove BuildEdit/Add

Simple, clean, and your not using any goofy Get Player Controller (0) nodes.

Ahh F9 is super helpful, wish I’d known that sooner!

Still getting error without calling Destruct, once Destruct is called error goes away and PlayerController is valid.

I’ll try moving some things around and see what happens. Thanks!

Just realized I’ve been mispeaking. Most of my logic is in Character and I hardly have a PlayerController.

And I ended up moving IMC to Character and it fixed the error!

Is it okay to handle IMC in Character if I’m designing a singleplayer game?

It’s OK, but you’re better off loading IMC in the controller. Controllers are persistent, characters are not.

Simply needing a reference to the controller to implement IMC tells you where it should go.

1 Like