While going through a list of "debuffs", what would be the best way to add a new one if none are here or extend duration if it already exists.
In my game players have the ability to inflict "debuffs" on each other. When one is supposed to inflict a debuff on the other player I loop through a List to see if that debuff already exists there, if it does I want to either extend the length, or maybe add to it's effect. What would be the best practice way of doing this? Right now I am doing the following:
var debuffs = List<Debuff>();
bool didExist = false;
And further down:
foreach (Debuff debuff in Debuffs)
{
if (debuff.GetName() == "debuffName")
{
debuff.AddDuration(10);
didExist = true;
}
if (didExist == true)
{
debuffs.Add(new Debuff("debuffName", 10, 10); // name, duration, effect
}
}
I feel like this can be done easier somehow, but not quite sure how. Surely I should be able to leave out the boolean?
Comment
Your answer
Follow this Question
Related Questions
c# foreach does not work 1 Answer
c# foreach does not work 0 Answers
C# Foreach element[,] in list problem 1 Answer
Playing Through a Queue/List of Audio Sources, One at a Time 1 Answer
How to remove an item from a list of custom variables 1 Answer