Get variable by name(?)

First of all , i apologize for misleading tittle, because i dont know how to describe it.I just started using unreal engine 4.

I have a player actor which has “Target” actor reference variable. The target variable is changed by gameplay blueprint (selected) and I have enemies called “Enemy 1” , “Enemy 2”.
I want to get variable from actor which is in “Target” Variable.
how i can achieve something like that on bottom?

Note: im not asking why it wont allow connect , i just wanted to show how i want it look like.

You will need to cast the “Target” variable to your specific blueprint type which has added the “Max Health” variable.

Right now your “Target” is a regular actor and doesn’t ‘know’ about this variable called “Max Health”.

So drag “Target” to an empty space and type “Cast”, in the short list you should find your “Enemy 1” (or Enemy 2 as seen in screenshot) blueprint class, select this then you can connect the appropriate pins (the blue one on the bottom) to your “Max Health”.

i would want make it automaticaly read the variable when its enemy 1 or enemy 2. enemy 1 and enemy 2 has same variables(variable name) and i want them to be read when they are selected.
at the moment you show how to get variable from specific.
If i do thing what you told me, it will only read max health from Enemy 2

You might want to make a middle man class then, which does practically nothing more than add the “Max Health” variable, then base both Enemy1 and Enemy2 off of this middle class.

Then you just cast to your middle man class whenever you need to get their maximum health (or any other shared variables)

so i have to create a blue print which changes his variable equal to enemy 1 or enemy 2 max health when selected ?

This is hard for me to estimate as I’m not entirely sure what you’re trying to achieve gameplay-wise.

atm i want to show “Target” name ,max health / health on hud.
i dont want to create new casts every enemy type i have

So you do as I have described:

  1. Create a common “EnemyBase” blueprint
  2. Add health/maxhealth/name variables to this blueprint
  3. Create “EnemyType1” based on this “EnemyBase” blueprint class
  4. Open the “EnemyType1” and set the defaults as desired (different maxhealth values and names)
  5. Whenever you want to get information about this enemy, you can just cast to EnemyBase and you will be able to get their name basehealth/maxhealth values

Optionally you want to add a “BeginPlay” event on your “EnemyBase” class where you set Health to be equal to MaxHealth, this way it will automatically set an enemy to it’s full health potential.

This is an approximation of how blueprint inheritance works with variables:


im confused at

then base both Enemy1 and Enemy2 off
of this middle class

, how i can get variable from Enemy1 using EnemyBase, have i use blueprints or there are settings in this

Because every Enemy1 is also an EnemyBase.

Here is a quick video where I show how I create inherited EnemyTypes which all have the MaxHealth variable (but each type can have it’s own default)

I do this in a couple of ways to demonstrate how you can change a blueprint’s parent:

  1. Create child blueprint via right click on EnemyBase in content browser
  2. Create a new blueprint and select EnemyBase as it’s base by searching for its name
  3. Create a new blueprint of actor type and changing it’s parent by going to the class settings

works like charm thx

Sorry I seem to have edited the post which you accepted as answer (youtube link instead of onedrive) and now the answer status has reverted …