Assuming I dont use interfaces
So lets say i have a line trace where i can shoot a weapon from. So there are now barrels and humans and other destructible items in my game world. How should I approach casting that? So for humanoid i wanna cast to enemy NPC and for destructibles I want to cast to destructibles. Should I make a parent class for both and have the parent have receive damage event, but the thing is those 2 are two entirely different things. My current solution is using a sequence with different cast to but I am wondering if thats a recommended approach. So i am casting to enemy npc then the other sequence is casting to destructible and the other sequence pin is casting to another object etc.
- I am also wondering what would be the cleanest way to handle line traces with different set ups. So for instance i want to be able to holster the weapon and use the line trace to talk or interact rather than damaging objects. Should I use a switch enum for that? Also use an enum for the weapon type? (Different weapons have different ranges etc). If someone knows a tutorial or has tips how to handle this without having to cast to each and every class, it would be awesome.
Ah so i should just create a base class like „destroyable/interactable object“ and then abstract or branch out 2 classes: 1 enemie subclass and one prop subclass.
Thanks for taking your time to help Nacho!!! 
The problem is that you can’t do that in blueprints because humans inherit character and barrels from actors.
Everything that explains NachoMonkey2 already exists outofthebox, and it is the damage events.
Apply damage without casting to the hit actor and use anydamage, Point Damage or Radial Damage events to put the different logic on each object
1 Like
Perhaps then just use a sequence with multiple cast to nodes?
Yea thats the thing I am trying to wrap my head around. For instance lets pretend i wanna use left click to damage some objects and with others i want to interact, like basic light switches or initiate dialgoue. I think interfaces would be best but i am wondering how one would neatly do it with casting alone. Have a sequence node with bunch of casting? Like cast to „enemy“, cast to „objectprop“, cast to „npc“ to talk etc. Now all three could have different parents and are different classes entirely. Just looking for a way to efficiently do it; or want to know how i would deal with it if i only had to cast.
Note: I am asking purely for educational reasons as I am trying to understand and leverage my knowledge on every communication method.
Without going into UE4 api; in c++ would you handle it by checking if(actor == hitactor) and then go from there? So bunch of if statements that deal with every hittable object. Am I right to assume casting is a bp thing?
Yea I get interfaces. Like mentioned, I am just curious how one would approach a system similar to interfaces if only casting was at disposal.
You can also use an actor component and manage all damage-related logic in there. Make an actor component called “DamageComponent” and add the necessary variables and event dispatchers to it. In any actor blueprint, you would then add that component; it’ll look like this:

To apply damage, you would get the damage component using the GetComponentByClass (the one that returns a single component). Then you would call an “ApplyDamage” function in the DamageComponent to apply the damage, which will subtract from the component’s health and call OnTakeDamage, or if the health is depleted, OnHealthDepleted.
The advantage with this over interfaces is that the component handles all the logic itself, including ticking. This allows for more things to be possible. For instance, say you wanted every actor to play a sound and shake whenever it takes damage. With interfaces, you would have to set this up on each actor that implements it. But with a component, you can do it from the component, and it will be applied to every actor that uses it automatically. The OnTakeDamage event would then be used for actor-specific things.
1 Like