(UE5) I have problem with understanding *cast to* and error in basic project. Please, help


Hello, i have a simple project based on default fps starter project. In rifle BP i make int AmmoCount variable. I also make widget interface with text block, it’s should show my ammo count. I trying Cast to and have a problem, idk what to connect to object. Please don’t offer me another way to implement my idea. I need exactly what I’m trying to do. Tell me what to do with Cast to or explain why my attempts are hopeless. You can also try explaining to me why I’m using Cast to incorrectly if I’m really not using it for rational purposes. I apologize for my ignorance.

You don’t have to cast.

You need a reference to BP_Rifle in your widget.

You can get it with ‘get actor of class’.

1 Like

Good to see you got it :slight_smile:

You might be wondering why you didn’t have to cast?

There’s a LOT of misinformation floating around youtube. You will hear things like “we need to communicate with that blueprint, so we have to cast”. BZZZ! Wrong.

You’ve probably heard of blueprint inheritance by now. You know, you make a parent blueprint, and then you can make child blueprints from it. It’s the kind of thing you do when you don’t want to keep making the same code over and over again.

So you make a gun BP ( for instance ) that has all the stuff all the guns have, and then you only have the bits that are different in the children. Like, it’s a different shape, or sits in a different location in the hand socket etc.

Now: You make a gun variable ( reference ) in your player. You only need to make it of the parent type, because that will automatically work for all the children. Your player can just have the generic ‘gun’ reference, you don’t have to worry about which type of gun they have just picked up etc.

But: When you do want to know what kind of gun it is, that’s when you cast :slight_smile:

Casting is just a way of finding out which child BP ( kind of gun ) your player is holding.

So you can see, you will often have to cast at the same time as communicating, but you don’t need casting to communicate.

Hope there’s something helpful there.