Retrieve variable value, from various possible classes with the same variale

Hello everyone!
(Posting this in ue 4.21 but i think this applies to any other 4.2 version)

First a little context:

I am working on a old style final fantasy-like battle system, where the map has 10 slots, and they get occuppied by 5 enemmies and 5 player characters (controleld by 1 player).
When the player gets transfered to the battle map, the game sets the enemmies to ocuppy slot 1 to 5 based on a randomized variable, or preset variable (depending if its a random encounter or a storyline battle) and occupies the slots 6 to 10 depending on the members on the player’s party.

Both the enemmies and player characters are pawn classes, with skelletal mesh, cammeras, variables, etc. they all have float variables named, i.e “Health” and “Defense”.

Now to handle the variables as global, i have a gamestate class called “playerslots” that contains variables named “party#” and “enemy#”. These variables are accessed by the battlemap blueprint to know what players and enemmy class pawns spawn in the battle map, that part is working all good.

Now, “playerslots” also contains variables named “player#health”, that i need to fill accordingly with the “health” variable of the pawn class on that character, NOW i can achieve this using the “cast to Xplayer battler class” node and retrieve “health” variable from there and set that to “playerslots” using the “cast to playerslots” node. IT WORKS. But for that i need to set a large array of “if” conditional branches to check wether the “party#” variable equals sussie, or anita, or clementina, etc etc etc. In the case of the player characters this is not a huge issue since they will not be more than 8 options or so.
The problem comes when the game will have a lot of possible enemmies where to check, and accordingly to that enemy, a LOT of “casto to this enemy” node in order to assign the “health” and “defense” variables from that pawn class into the “playerslots” and then be used by the battlemap blueprint.

The problem could possibly be solved if i could somehow use the “cast to” node to whatever class it’s actually in the “enemy#” variable, that is a class variable, whatever class the variable is currently filled in with will be a pawn class, that contains a float variable named “healt”
[enemy#]->[cast to (this)]->[get health]

is possible? if so, how? if not, then how could i solve this withut using 100+ conditional “if” branches and everything that comes allong with them?

Thanks in advance!

You should check the concept of inheritance. It lets you do exactly what you want without all those casts. You may want to use either a common parent class or interface.

Parent class is nore preferable if the classes are of the same type, like your enemies. They may be monsters or robots but they are all can be of type BaseEnemy.

Interfaces are needed when classes have some common parts but they are not really related. E.g. you want to be able to apply some effect on different units, let’s say when you apply “slow” ability, it slows enemy units’ movement speed or slows enemy structures’ production speed. These two are of different types but you can create a common interface “Slowable” for them.

Use what is more appropriate

Hello Tompson, I’m still new to UE and i haven’t worked with parent-child classes but i know them, what i don’t know is if child classes can be also pawns with different skelletal meshes and materials?

Helo Hustlan,Thompson recommends the same solution so i’ll have to try it out, as i said i’m still new to UE and have never worked with parent-child classes.

As for the instances, each time i “spawn actor from class” that gives a separate instance, right?
How else can i spawn an instance of the child class from a parent class? I trully don’t know

yes they can have different things like meshes. lets look at the character bp as an example, the character class defines that its children will have a skeletal mesh but thats all so you can change the mesh as you like, the same goes for variables if the parent has the variable the child will too and the default value will be set to the parents default unless overridden in the child. once you understand hierarchy things get easier to understand. my favorite analogy for hierarchy is cars, if the parent class is car which defines basic attributes 4 wheels, engine, etc then the children will have those attributes but can also have their own as well. the children add more details to further define the class so in the car example you could have a child camaro, then a child of camaro could be the specific models.

the basic thing to remember here is the children have all the functionality of the parent but not necessarily the values.

You’ve given me a little doubt there: The skelletal mesh of the child can be different from the skelletal mesh of the parent?, i.e an enemy might be a 4-legged creature, another a snake-like creature or a biped one, therefore i’d need different bone sets.

you don’t add an “enemy” to the parent; it is just a foundation. You just add a skeletal mesh component without defining it then when you make a child you can define that child’s skeletal mesh as a snake then make another child for bipedal, so on.

The parent only has the component not the skeletal mesh it-self

pretty much like isaac said you add a skeletal mesh component but the component can be set to use any skeletal mesh. think of it like a variable, the variable exists but its value can be changed.

its easy enough to test and you will understand better then. make a character and set its mesh, then create a child of that character and try to set the mesh in the child to something different than the parent via the details panel.

You spawn children from child class, not from parent. But children instances can be cast into parents and can access all (for the most part) parents’ members and methods, this way you only need one cast into parent instead of many casts into different classes.

As I said, learn the basics of inheritance - it’s really important

Seeing it as a variable indeed makes it clear to me, thank you all for the eplanation, can you convert your first comment, Thomson, into an answer so i can sellect it as the solution?

Thank you Hustlan for the explanation, i start to see it clear now, will have to put it on the engine after when i’m on the pc for it. Thank you very much!

if i understand what your asking right your trying to have a single cast node that casts to a variable class. put simply thats not possible. just think how would that ever function.

the solution to your issue though isnt all that hard, have all the enemies inherit from a base enemy class which has the needed variables. this will allow you to cast to the parent class instead of casting to the individual enemy classes. so again you would want a base enemy class which contains variables for health, def, dmg, etc then make each specific enemy a child of the base so they inherit the variables but they can set the value to be specific to them. you could have a rabbit with health 10, and a mecha with health 5000, they would be the same variable since they are inherited and both enemies the rabbit and the mech when casted to the base class will be successful.