Casting from a button widget to a character BP?

Hello, i’m new to UE, so apologies if this is a stupid question. I’m trying to cast to an NPC blueprint i have, so that i can update the STR stat of the NPC when the “PushDrillButton” button is clicked. the NPC class is character so i passed a character variable into the cast, but i’m just getting “Cast Failed” when i press the button, what am i doing wrong? thanks for any help!

My first guess is that the Character variable is null. Can you show how it is set?


so this is what i’ve done, it’s just defined as the character class, as the NPC BP is a child of the character class? not sure if that’s right or not! thanks!

You need to somehow assign a reference of the instance you want this to affect.

I’m assuming that by NPC you have more than one pawn in the world. What actor creates the widget?

so i only have one NPC pawn in the world - and it’s in the world when i start the game. the widget is called when i press a button… (so i press a button to create the button that will update the STR) -


I’ve also tried just referencing the NPC directly in the widget blueprint - interestingly this worked for other widgets where i was just writing the STR to the screen (see below) but doesn’t work for the above BP? i’m so confused!

Thanks for any help :slight_smile:

Ok. This is how I picture it would work:

  • On button press the possessed pawn, the one that you can control, gets a reference to the NPC.
  • The pawn creates the widget and binds to a dispatcher that calls on pressed.
  • When the binded event / function executes the pawn then affects the NPC with the desired.

So the widget doesn’t have to cast to any actor, the possessed pawn is responsible for getting the reference of the NPC, creating the widget and waiting for the button press.

Makes sense?

sorry, i’m not sure what you mean by this - apologies. the NPC is actually the only character in my game. it’s sort of animal raising game, where you don’t control the animal, more sort of just oversee it, so there’s not a pawn that is controlled -

i’m also not sure what a dispatcher is - sorry i think i have a lot to learn!

Perhaps not a traditional one but there has to be a possessed pawn. A class is needed to handle how the player interacts with the world. Maybe you haven’t assigned one yourself but the engine still provides a default on play.

On play press F8 to decouple from the possessed pawn, there should be one floating around, should also be visible in the outliner with yellow text.


I appologize if I’m making this more complicated than what you had intended. The button could workd by simply using Get Actor of Class to get the reference, but it won’t get you far…

Here is a simple project of what I suggested. It’s how I understand communication would need to be setup to interact with different actors.
SelectColor

SelectChangeColor.zip (112.8 KB)

Everything is in the Blueprints folder.


I suggest you take the time to check out these free resources:


We all do! Nothing to be sorry about. Just be patient and go at it on your own pace.

I just hope I’m being more helpful than daunting.

Thank you so much for your help, i really appreciate it - i am rebuilding the project from the ground up so will work around this, but thank you for taking the time to explain - it did help!