Object ref not valid in AnimBP

I’ve got a problem.
When I cast to the Player Character it says it’s valid but when I use it in the Update Animation it’s not anymore?
I’m trying to get the camera location from the Player Character.

Begin Play prints Valid but after that it just prints invalid. why?

I’ve tried it with get actor of class, get all actors of class with index 0, get player character even set up an interface from a working AnimBP with the vectors in the message. The object ref is valid until I use Update Animation.

The same setup works in another AnimBP but why doesen’t it in this one?

well, I don’t know what the GET node you’re using in the Update Animation is, but my guess is that it’s getting the “As BP Kellan” variable. Which is set to the result of GetPlayerCharacter(0).

If this AnimBP begins play before the player character does, it will just be empty, the whole time. If this AnimBP caches that GetPlayerCharacter(0) result successfully, then tries to use it at some point after that character has been destroyed, then it would no longer be valid.

There’s not a clear association here between the ABP and GetPlayerCharacter(0), so you probably shouldn’t be accessing GetPlayerCharacter(0).

What are you actually trying to get to use with the UpdateAnimation? The character that the ABP belongs to? In that case, if you’re using a Pawn or a Character, you can use TryGetPawnOwner, or if you’re using some other actor that has a skeletal mesh component, you could use GetOwningActor.

Okay, so im trying to get a ref to my Player Character “Kellan” from an AnimBP that is implemented in a diffrent Character. The other AnimBP in which my solution works is implemented in the PlayerCharacterBP maybe it works because of that. What would be the best way to solve that?

From a design standpoint, reaching out to an object that you don’t really have a reference to, has a bit of a ‘smell’ to it, like are you sure you need to make something in the ABP for another character dependent on something in the Player Character?

But you should be able to use GetPlayerCharacter(0) just don’t cache it in begin play, because the result of GetPlayerCharacter(0) may be different at any given call.

1 Like