Get Owning Player Pawn returns none on Widget attached to component

Hi Community,

I have a widget that sets player’s health bar. I use Get Owning Player Pawn to retrieve my Base Character in which I later use in my bind method to do Health Logic for the UI.

This works when the widget is set up to a HUD object which is then set as the HUD in the game mode. However when I assign the widget to a WidgetComponent, the Get Owning Player Pawn always returns “none”.

Is there something special I need to do in this case? Any help would be much appreciated!

I’d suggest an alternative approach. Create a variable of type HeroCharacter in your widget BP, and set it when the widget is constructed and added to viewport. That way you’ll always ensure that the value doesn’t come to null

Thank you for the response, however I am not sure I understand. Currently I am trying to set the HeroCharacter variable in my Widget BP. The above scripts are connected to the “Event Constructed” event. If the Widget is instantiated by a widget component, the “Owning Player Pawn” returns none, but if its instantiated by the HUD or directly in code, it works no problem.

Thank you for the comment!

Are you referring to dragging the component into Widgets BP script or into the Players BP script that contains the component. I must have forgotten to mention that the above scripts are in the widget BP’s “Event Construction”. I am trying to keep an instance of the Hero character in the widget BP so I can do look ups on players healths.

Not exactly sure what the set-up is for component widget in your BP, where everything is located etc BUT if “Hero Character” is the PLAYER character that you are using and you want to modify health variables within then why don’t you just call “get player character” or “get player pawn” that will return the instance of the player character (If using a character template BP) or player pawn (If using the pawn template BP) and it should work fine. I wouldn’t go for targeting the “user widget” to then get the owning player if the “owning” player is the character you are using, there are much easier ways to get a reference to it as I have mentioned above.

Well the setup is as follows. There is a WBP_CharacterInfo Widget, that contains a health bar. The widget is used in two places. It is added to a WBP_HUDLayout Widget, which is then added to a HUD blueprint. The second place it is used is on a widget component for enemy characters. I was hoping that within the widget blueprint script I would be able to get “Owning Pawn” check if its of type “BaseCharacter” (Contains the current health information of both Actor) and from there update the health bar as required. I believe calling “Get Player Character” will be an issue since its assuming that this WBP_CharacterInfo is only tied to the player character, but what I want is the WBP_CharacterInfo to work with any BaseCharacter Pawn that it’s attached to. My apologies if I am still not understanding something.

Well, the Health logic is all taken care of in the BaseCharacter C++ class. The base character exposes two methods. “Get Health”, and “Get Max Health”. The idea is that the Widget doesn’t need to care how the health is calculated or how it is updated. It just needs to calculate the health percentage every tick so it can update the progress bar to reflect what the health currently is (Image below is my health progress bar bind function).

So now, what I thought I could do was have the Widget contain a reference to the Base Character that is currently used it. So my approach was on widget construction event, I would get the pawn who owns the widget and if it is of Base Character, we assign it to the variable. I thought “Get Owning Player Pawn” would work for all cases, but it fails when the widget is apart of a widget component. What I think you are trying to say is that the widget should not be setting this reference but its the responsibility of the actor that is using this widget to set this up. Or have the actor pass its health percentage to the widget itself.

No, not at all. I’m just trying to understand your set up. I’ve made “parent” enemy blueprints, and used a widget in the components panel that is a health bar and when you attack the enemy the health bar goes down. My issue is not that I couldn’t tell you how to do that it’s just however I did it doesn’t have these issues you’re having. And everybody likes their code a certain way so unless you come to me with no starting point I really only tell people to change their code if it is blatantly bad logic. That being said, if the widget is a component of an actor blueprint whenever you have an event that requires updating the health bar of that actor, say on overlap of a player weapon it shouldn’t be hard to cast to the enemy overlapped and modify it’s component widget. So I’m not understanding why you need to find an owning pawn on construct. It’s hard for me to see your logic with that. I think you’re making this more complicated than it needs to be unless I’m missing something about your game mechanics?

Yea pretty much. I think it is easier to go from the character/pawn/actor that actually has the widget component TO the widget. Since the widget is already a component of that actor why have the widget try and figure out who it belongs to? The actor you placed it on already knows that information. All you need to do is get the specific instance of the widget in the actor BP which if i recall is done with dragging out the widget component into the editor and pull of that pin and search for “get user widget object” that retrieves the specific instance of the widget and from there it is easy to access anything you want within the widget and pass health variables from the actor to the widget.

And yes, even with this method the widget doesn’t “calculate” anything with the health it is just told what the health is and can do a basic division to “get percent” or that can even be done in the character BP or when damage is taken in a weapon BP or something. Either way the widget doesn’t really do much, it just passively receives the information about actor health and updates a progress bar.

I see! Thank you so much for taking the time to walk me though that. I will update my logic as needed.

Really appreciate the help and patients :). Not sure how to make these comments as answer.

I will do it. And thanks, let me know how it goes.

I have had this issue before, I forget exactly what I did but I believe you have to drag the widget component out into the editor and then from that say “get user widget object” I believe is the node and that gets an actual instance of the user widget.

Lollalol’s solution solved my problem.

Go to your widget blueprint, add a variable called “Owner” (of whatever type owns it, HeroCharacter?) and either expose it or create a setter function for it.

In the blueprint that you’re creating the widget class and adding to the viewport: after you construct the widget class, drag its’ output, and right-click to either set the “Owner” variable (either exposed, or using the setter if you created one).

Go back to your widget blueprint, and instead of using the “Owning Player whatever” node, drag out your “Owner” variable, and use that instead.