How to handle damage with multiple types, using functions?

Hello! I have a general optimization question, regarding functions and input.
Consider an elemental damage system. There are different damage types, with different logic and variables each. However, they all share common attributes (damage value, source and target). I am making a health system Actor component. I thought one way to approach this is having all the logic and input in one function and use an enum to select a type like this:

however, it seems that too much unnecessary data will be loaded (from input), because all the inputs referring to the unused types will not be used.

Another approach is making many functions for each damage type like this:


but still, there are many things repeating (those that are common for all damage types).

What is the best approach to this? Is there something I am missing?

Thanks in advance.

Your question requires a very complex answer because there are so many different ways to do it. The good news is, whatever way you decide to do it will be fine because you will mold it to fit your approach. Don’t confuse quantity of code with optimization. The fact that you have repetitive functions does not necessarily mean you will suffer gameplay performance. You will just suffer the chore of making changes in many places but that chore will make you learn more about how to model the code. Either of those solutions you posted would work and starting with either one will likely be fine.

Don’t go down the rabbit hole of perfect code. It gets in the way of making a game. Pick a way, do it, step back and evaluate what you don’t like. But evaluate it by hitting “play” and watching it work, don’t evaluate it by looking at the blueprint. The most important part is how it feels. If there is something you don’t like, when you go to fix that you will then learn what you don’t like about the code and you will enhance it so adding gameplay changes will be easier.

Hope this helps in some way. Good Luck!

Thanks for your response. I just looked at my situtation and it seemed a fairly easy thing to code, but when I looked at my code I just thought that I am missing something much simpler, and I was afraid that this was not a good, maybe even a bad way to do it. I guess as long as it works and doesn’t make my computer go crazy…it’s fine.

Ofc if anyone knows the optimal way, I’m willing to start over.

I would make a base class with the common functionality, and then add virtual functions to extend in the base class.

I would then inherit from the base class and add functionality to the child classes that are specific to the element type.

Damage is common to all of the functions, and “freeze time” could be abstracted to “effect time”.

I’m not a blueprints guy, but this would be fairly straightforward in C++.