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?
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.