Hi there. I’m currently having trouble trying to access my third person character inside of an actor class.
So I’m trying to make a weapon (gravity gun) that the player will be able to use when they pick it up. However, in order for it to work the way I intend, I need to be able to access parts of the character BP like the capsule collider. I originally did this in a first person BP and it worked much easier but that’s because I was creating it as a new class so now that I’m creating it as an actor to be attached to the character, I need to cast to the character. However as you can see from the images above, I am getting compile errors for the parts of my gravity gun script where I try to access the capsule component.
The error reads “Variable node Get CapsuleComponent uses an invalid target. It may depend on a node that is not connected to the execution chain, and got purged.” as well as another one “This blueprint is not a character, therefore Target must have a connection.”
Any help with how to fix this and how to access the third person character in an actor’s BP would be much appreciated. Thanks!
As a follow up example of the same problem, I am here trying to make the pickup actor work so that when it interacts with the player, it will cast to the player and set a boolean to true. It will then destroy the pickup icon. As you can see, I am having the same compile errors here. I know the problem relates to not having an object connected to the cast. But I have no idea what to use as an object, as in both cases, I am checking the object for which we are currently inside. So I tried therefore using the “self” option, which then says that the character “doesn’t inherit from the pickup”. This is a really annoying problem that is stopping me from getting on with the rest of my level, so I’d really appreciate if anybody’s got a clue on the issue here. Thanks again
Casting node (and type casting in general) is used to reinterpret the object of one type as the same object of another type. The key here is that it is the same object, but after the cast it ‘appears’ as if it is of another type. In case of the player, this means that the ‘GetPlayerPawn’ node will return the player’s actor as if it was of the Pawn class. However, to be able to access the functionality you implemented, you need to access it as if it was of ‘ThirdPersonCharacter’ type.
I’m explaining this because understanding this will help you understand the required layout of nodes to get this right. For example, in your last example, all you need to do is to connect the Return Value pin of the’Get Player Pawn node to the Object pin of the Cast node. The Cast node will then take the Pawn interpretation of the player as the input and return the ThirdPersonCharacter interpretation to the output. Note that both are the same object, just the type changes.
In your first example you are trying to cast GravityGun to the ThirdPersonCharacter. The gravity gun is not in any way related to the Player - they are two completely different objects. You need to use the GetPlayerPawn node in its place.
The key takeaway here is that you can cast only to types connected by inheritance chain, and the object doesn’t change.
Thanks mate, I think that does a really good job of explaining the casting to me. I’ll try figure out the rest of it as I go along. Much appreciated!
My only other question then is what to do in this situation? I am trying to add an impulse coming from the character in order to shoot the held object forward. I’ve now changed it so that the character cast comes from the pawn. But the error still remains with trying to use the capsule component of the character into the GetForwardVector function?
For this one, try connecting the Return Value of the Get Player Pawn node (bottom left of the screenshot) to the Object pin of the Cast node.
Oh no sorry, fixed that one. I uploaded the wrong image. I meant this one down here
I tried connecting the GetPlayerPawn instead of the gravitygun as an object, but it says the capsulecomponent is still not allowed for the connection. I tried using the camera and everything too but it doesn’t seem to work.