How to reference shared character variables in an animation blue print

You want to have both player and bot derive from a common class. The animation BP can cast them to that base class and you can put all the stuff it it needs in there. This also works out great because then you don’t need to have an unused camera in your bot and you don’t have any unused AI logic in your player. I set my game up like this and it’s beautiful.

My player character and enemy characters have very similar locomotion. Basically they’re both humans who run around, jump, and shoot guns. I’d like to setup both my player character and enemy to use to same animation blueprint since they would need most of the same setup. This works pretty well except when it comes to referencing variables on the character from the animation blue print, variables like “isDead”, “isSprinting”, “WeaponDrawn”, and so on. I’m currently pulling these variables by casting to the player character and setting a local variable within the animation blueprint. Because I have to cast specifically to the player character this cast fails when its setup on the enemy character. A work around i’ve been using is to cast to the enemy character if the player character cast fails, but I would have to do this for every variable I need to reference for each character class I want to use this anim blueprint. Is there a better way to reference the variables on a character from the anim blueprint?

And then any shared variables need to be setup in the common base class? I’ve already built most of my hero, is there a “good” way to split up the hero into a common and derived class? Maybe this is a dumb idea but could I duplicated the hero class then derive one from the other and delete out whats not needed by each class from there?

Yes. The base class has everything that the two derived classes have in common.

Back your project up because it could get messy, but it should be possible to use one as the base class of both and then derive the other. When you reparent a blueprint, it will rename any variables/functions in the derived class with the same name as one in the base class. The problem is all the nodes in the derived class will be tuned to the derived class’ variables. Like, you have a variable Foo in both then you will get Foo in the base class and Foo_1 (or something) in the derived class. All the nodes in the derived class will be set up for Foo_1. In many cases you can just drag-drop the base class variable onto the derived class variable in the graph and it will ‘rename’ it. Sometimes it might not be that easy though, and regardless, it will be tedious. I set my project up with a common base class beforehand but I have to do this kind of conversion all the time, like when I move some blueprint functionality into a C++ base class.

I suggest doing it bit by bit and making sure it still works at each stage. Or, the other option is to reimplement it and use what you have as a guide. Depending on your project, it might be faster and less painful. But for sure, this method is the right way to do it. So many tutorials just duplicate everything. AI with cameras… Ugh. :slight_smile:

One more tip: when you do it this way, you can also have your AI use the base class instead of the player. Then your AI can fight each other too.