Cast Charater to Child Class


I’m kinda new to UE4, and I’m a little confused about something, since it doesn’t seem to work the way I expected.
I have created 2 classes: “Enemy” and “Thief”. Enemy is a child of “Character” and “Thief” is a child of “Enemy”. Inside my “Enemy” class, I have the function “attack”, which I implement in the “Thief” class.

I also have a boolean variable in the “Enemy” class called “IsAttacking”, and I also have a Decorator for my “Thief” Behavior tree that checks if the thief is attacking by picking the value of this variable. So, if my thief is not already attacking, that means he can attack.

The thing is: I want to use this same decorator for other enemies behavior trees. I thought I could pick the “controlled pawn” and cast it to “Enemy”, so i can get the “IsAttacking” value, and then do the same to call the “attack” function, without ever knowing which child enemy class I’m telling to attack. This doesn’t seem to work, though, since the cast always fails.

I mean, I can cast “Pawn” to “Character”, but I can’t cast “Character” to my “Enemy” class, so I can acces this variable/function? Why? I thought I could do that since “Enemy” is a child of “Character”, and the Object referenced is a “Enemy” instance… (I’m thinking on the “Animal” to “Dog” cast example)

This means that you either input null (which is called “None” in blueprint) or object of unrelated class in to Cast node. Place a breakpoint on Cast node and run the game trigger the cast, game will stop and go to blueprint, when you hover over the input pin it will show you what object is going to input which should hint you why it is failing.

Well, the input doesn’t seem like it’s a null, so maybe it’s the second option? But to be honest, I have no idea. As I said, since I made my Enemy class a child of character, I thought I could cast a character reference to an “Enemy” reference. Btw, I also get the error “Error Accessed None ‘K2Node_DynamicCast_AsEnemy’ from node Return Node in graph ‘PerformConditionCheck’ in blueprint CheckIsAttacking” (CheckIsAttacking is the class name)

I don’t know if this helps, but here’s the graph: