如何防止同一类型表单的对象同时添加到列表中



所以我有这个代码,它只允许在尚未添加debuff的时候应用它,但问题是,如果同时调用代码(添加debufff的射弹在同一帧命中(,那么debuff会添加两次。

public void AddDebuff(Debuff debuff)
{
if(!debuffs.Exists(x => x.GetType() == debuff.GetType())) // prevents same type of debuffs stacking
{
//Debuffs are first added to newDebuffs
newDebuffs.Add(debuff);
}

}

我想我通过在if语句中添加另一个条件来解决它:

据我所知,没有更多的意外行为。

public void AddDebuff(Debuff debuff)
{


if(!debuffs.Exists(x => x.GetType() == debuff.GetType()) && !newDebuffs.Exists(x => x.GetType() == debuff.GetType())) // prevents same type of debuffs stacking
{
//Debuffs are first added to newDebuffs
newDebuffs.Add(debuff);
}

}

问题是,它只是检查当前debuff列表中是否已经存在debuff,而不是检查要添加的debuff名单中是否已经有debuff。

因此,当要添加的debuff列表移动到包含当前debuff的列表时,它会传输同一debuff中的多个实例。

最新更新