Unable to cast properly in a Widget Blueprint

Hi. I’m building a project with c++ in 4.20.
But I’m using a Widget Blueprint to bind a progress bar.

I’ve got a Player Ship class that extends Pawn. And a BP_PlayerShip blueprint is attached to the c++ PlayerShip class.

In the Widget the function is called GetPercennt_0.

I’ve set it up as:

GetPlayerPawn(index 0) -> CastToPlayerShip -> Get Health -> Return Node

But I keep getting the error on the Get Health node:

“This blueprint (self) is not a BaseShip, therefore ’ Target ’ must have a connection.”

I understand the Widget Blueprint isn’t a BaseShip but I thought GetPlayerPawn would get the default player pawn.

I’ve got a class called PlayerShipGameModeBase that extends gamemodebase and this is attached to a blueprint called BP_PlayShipGameModeBase. Wasn’t sure if I have to reference the game mode to get the PlayerShip.

Thanks.

I can’t see your code, so i have to guess:
My guess is that you forgot to plug the casted PlayerShip object into the “target” input of the GetHealth node.

GetPlayerPawn has a blue output node that gives you a pawn object, you plug that in the CastToPlayerShip node.
CastToPlayerShip has two output paths and a blue output node that either is NULL with output path “Cast Failed” (if cast failed due to the object not being a PlayerShip object) or the blue output node is a PlayerShip object and the output path is the top one.
You only want to continue if the cast is successful, so connect the top output path of the CastToPlayerShip to the GetHealth node. And you also need to connect the blue node from the CastToPlayerShip cast to the blue input node (the “target” node) of the GetHealth node so Blueprint knows what PlayerShip to get the Health from.

The target input node is used in many blueprint nodes to indicate what specific object you want to get values from, call functions from or set values for. If the “target” input node is left empty/without any lines going towards it, then Blueprint assumes you want to use by default the Blueprint this node is in (hence the “self” input), which would be the Widget in this case, which does not have the “GetHealth”-function, so it fails.

Thanks! I uploaded the nodes. (Nb the class is actually base ship not player ship). Cheers

While you correctly get the health from the casted BaseShip, you didn’t connect the white execution flow nodes, so neither the getplayer pawn nor the casting nor the gethealth is ever done and thus the value at the return node is invalid (a function can’t return a value from another node that is never called).
To fix this:

  1. From the “Get Percent 0”-Function start node, connect the white arrow to the “cast to baseship” input arrow
  2. and from the upper “cast to baseship” output arrow to the return node.
    Since the casting could fail (if the player has no pawn possessed) then you also need to take into account what should get returned when the cast fails:
  3. Right click anywhere and select “add return node” and give it a default return value (like 0 or -1).
  4. Connect the “Cast failed” output arrow to that new return node.

Thanks Stefan. Sorted.