Can't figure out 'Casting'

Before I start, I’m sorry for the stupid problem,I feel it’s going to be really easy to solve, but I can’t for the life of me figure this out. I’m new to Unreal Engine 4, and, on top of that, not the best programmer around. I’ve been trying to get a simple gun blueprint to work, but I’ve failed. First, I created an gun blueprint, named GunTest, that inherits from actor blueprint. On the BP, I’ve made a simple method called “Fire”, and under the method, when It’s called, it prints out a message: “Hello”.

Imgur: The magic of the Internet ( The method )

Now under my character blueprint, I cast ‘Self’ to GunTest, which happens on BeginPlay, and, As GunTest, I call the method ‘Fire’ when the Input key ‘Q’ is pressed.

Imgur: The magic of the Internet ( Calling the method )

Most of you probably already figured out why it doesn’t work, but I’ll offer more details. When I run the engine, it says the blueprint didn’t compile correctly, I ran it anyway. I pressed Q, and when I got out of the game, it gave me these errors: ( Error in message log ) ( Error in blueprint Cast )

Well, from what I got out of it, if the gun was a child class to my character, this wouldn’t be happening. But I don’t want to make the gun a child class to my 3rd Person Character, since they share nothing between eachother. Is there any other way to do what I want to do without making the gun inherit from my character?

Also, if you could quickly explain what exactly casting is, that would be great. I’ve read around but I’m not sure if I’ve got the right definition for it.

Thanks for your time

Hi there. When you cast to a blueprint in unreal, you want to access information in another blueprint, but you need to specify what type of blueprint you are casting to. So when you want to cast to your gun, you need that type of blueprint, to be the object.
So if you want to cast to an animation blueprint, you need to type “get anim instance”, and if you want the game mode “get game mode”, and plug that into the object type. There are a lot of nodes like that for each different kind of blueprint.

But an actor is placed somewhere in the level, and there might be multiple of them, so you need to specify witch it is.

In thinking that the gun need to be attach to character right?
What I would do, is spawn the actor, from your character blueprint, and then attach it to the character.
If you type “spawn actor from class”, you get a node where you can choose your gun, as that class, and the output type from that node, is the same as the “as gun test” you get from your cast node.

Ah, I understand now. I didn’t actually make the gun attached to the character, I made it around the level just to test it out, and when the character called the fire function, the gun was supposed to shoot wherever it was. Considering the information you gave me, I might be able to fix it. Thank you so much for your time!

For some reason, I’m still getting the same error… I even tried spawning the gun from my character.

Solved: What I did was, keeping in mind what Thorreign said, I made a variable of GunTest in my Third Person Character blueprint and made it public and editable. I set the GunTest as the gun test object in the map, and it worked!

Your problem is not casting, but way you use the node, You getting “Accessed None” which means you inputing None (null) to one of input pins which can’t be None, in your case it is “Target” pin on Fire. If you get None to Target pin you will be calling function to something that does not exist, thats why you get this error. And this is not just cast node but any node with output pins.

Reason why you got None is because you getting varable from node outside of current line of events, output pin are like local varable, when when line of events all outputs get cleared. So either cast right before “Fire” or in Begin Play but store it in varable (which i think you should do if you plan to use it many times in that class) and use that varable to call “Fire” on it