- Home /
Keeping track of damage damage taken by which enemies
If I have an entity that can take damage from multiple sources, and I want to keep track of how much damage each source has given the entity, what is the best way to go about keeping track?
For example, I have an NPC, and two enemies. Enemy1 deals 5 damage to the NPC, and Enemy2 deals 6 damage. After that, Enemy1 deals another 5 damage. So I want to be able to check a field on the NPC and know that Enemy1 has dealt 10 damage in total.
The only way I can think of currently is to have a dictionary on the NPC with the Key being the enemy and the value being the total damage, and then update that value when more damage is taken. This seems a but clunky however, so I wanted to see if maybe I'm missing out on a more elegant solution.
Any ideas would be appreciated. Thanks!
Answer by AlvinIsCrack123 · Jul 14, 2020 at 04:23 PM
(Note : Before you start reading, sorry for my bad english. I ain't a native english speaker)
I recommend you checking this post which tries to find the better solution for storing values along a time (values can update on time progress).
"If the attributes that you are tracking are known ahead of time, then a class is the way to go."
So you could create a public static class (not derived from any other class) and put in the assets folder (to give Unity access to the C# script). Inside that script you put public static variables which corresponds to Enemy0_totalDamage // Enemy1_totalDamage... etc.
public static class GameVariables
{
public static class EnemyDamage
{
//And there you put your code/variables that stores the total damage of each enemy on player.
//(You will need to acess this script from other scripts to change values, obviously).
public static int TotalEnemy0_damage = 0;
public static int TotalEnemy1_damage = 0;
//And so on...
}
}
Thanks for the link and explanation!
Based on what the link is saying, since I don't entirely know how many things I need to keep track of, it looks like I should go with the dictionary ins$$anonymous$$d of the static variables.
Yes, unfortunately, using static is not the solution, it would mean that all NPC's would share the same info and that is not what you want.
Use the dictionary you had in $$anonymous$$d, it works just fine and is the most optimized way.
Your answer
Follow this Question
Related Questions
Dictionary Returning Null Value 1 Answer
OrderedDictionary in System.Collections.Generic for mobile 0 Answers
OrderedDictionary on Mobile 0 Answers
How to organize challenge system? 0 Answers