So basically in my game, there is going to be a lot of different monsters, and a lot of different bullet projectiles coming at the player. I want to check if the actor that hit the player has the projectile tag, and then pull the variable of BulletDamage from that actor to replace where the Damage variable is at the moment. How would I go about doing that as I do not want to cast to every single type of bullet in the game.
You don’t need to cast to every bullet type individually. Instead, make a parent blueprint with bullet type, damage, mesh, etc… variables, then make as many child blueprints as you’d like.
Doing that, you can just cast to the parent and still access the child data. You also wouldn’t even need to check a tag, since if the actor is NOT a child of the bullet parent, the cast will fail. It’s its own check
This is exactly how I would go about this. Parent classes with child classes are the way to go in blueprints.
So then if I took the Bullet Damage out of the cast to the parent, I could still change the Bullet damage in all the child actors and it would access that damage? To do that would I need to make a variable in each child with the exact same name, or do the variables get inherited by the children?
Sorry, I am quite new to UE4 and I never really used parents and children.
Yes, that’s not really a UE4 feature. This is a programming thing called inheritance. Like it was already mentioned before. All the variables that should be in all the different classes should be added to the “parent” class, and so, all the children would have the same property. Let’s say you want to create 3 different type of bullets, but they all would have a mesh, a particle effect, damage and velocity(?)… You name it. So then, you would create a base class, lets say BP_BaseBullet, with all these properties. Then you can right click that blueprint and create a child class from that and call it BP_TankBullet that would have an iron ball as mesh, damage of 30 and smoke as effect. That you would go back and right click BP_BaseBullet to create a new child and call it BP_RifleBullet that would have a small iron ball, damage of 5 and a different smoke as effect… And then so on… So basically, each child can have its all properties different from the parent and also from it’s siblings.
At the same time, all the children are also a “BP_BaseBullet” so you could cast any of the bullets to BP_BaseBullet and get the damage property from it.
This idea would also be applied to functions. You could create a function on the parent class, and override in the children. So when you cast to the parent class, it would call the children function…
just make sure that you override the function by clicking this button and selecting the function that you created:
This whole concept can be a bit confusing, but that’s a very useful concept to understand if you beginning on game programming.
ps: Whenever you do that with the variables, make sure you select the option to visualize the inherited variable, otherwise you wouldn’t see the damage. Like so:
Oh my god, I tried this way before, however I was wondering why I did not inherit any variables. Thank you so much everyone, you all helped so much.