Attempt to diagnose issues #278, #284 and #291. It appears that the game attempts to access the Limbs-field of a ragdoll after it's been set to null (which should only happen when the ragdoll is removed).

This commit is contained in:
Joonas Rikkonen
2018-02-26 15:06:00 +02:00
parent 064750d99f
commit 7f740c2f67
2 changed files with 21 additions and 9 deletions
@@ -13,11 +13,23 @@ namespace Barotrauma
{ {
partial class Ragdoll partial class Ragdoll
{ {
public static List<Ragdoll> list = new List<Ragdoll>(); private static List<Ragdoll> list = new List<Ragdoll>();
protected Hull currentHull; protected Hull currentHull;
public Limb[] Limbs; private Limb[] limbs;
public Limb[] Limbs
{
get
{
if (limbs == null)
{
DebugConsole.ThrowError("Attempted to access a potentially removed ragdoll. Character: " + character.Name + ", id: " + character.ID + ", removed: " + character.Removed);
return new Limb[0];
}
return limbs;
}
}
private bool frozen; private bool frozen;
public bool Frozen public bool Frozen
@@ -281,7 +293,7 @@ namespace Barotrauma
float scale = element.GetAttributeFloat("scale", 1.0f); float scale = element.GetAttributeFloat("scale", 1.0f);
Limbs = new Limb[element.Elements("limb").Count()]; limbs = new Limb[element.Elements("limb").Count()];
LimbJoints = new LimbJoint[element.Elements("joint").Count()]; LimbJoints = new LimbJoint[element.Elements("joint").Count()];
limbDictionary = new Dictionary<LimbType, Limb>(); limbDictionary = new Dictionary<LimbType, Limb>();
@@ -417,11 +429,13 @@ namespace Barotrauma
public void AddLimb(Limb limb) public void AddLimb(Limb limb)
{ {
if (Limbs.Contains(limb)) return;
limb.body.FarseerBody.OnCollision += OnLimbCollision; limb.body.FarseerBody.OnCollision += OnLimbCollision;
Array.Resize(ref Limbs, Limbs.Length + 1); Array.Resize(ref limbs, Limbs.Length + 1);
Limbs[Limbs.Length-1] = limb; Limbs[Limbs.Length - 1] = limb;
Mass += limb.Mass; Mass += limb.Mass;
if (!limbDictionary.ContainsKey(limb.type)) limbDictionary.Add(limb.type, limb); if (!limbDictionary.ContainsKey(limb.type)) limbDictionary.Add(limb.type, limb);
@@ -441,7 +455,7 @@ namespace Barotrauma
i++; i++;
} }
Limbs = newLimbs; limbs = newLimbs;
if (limbDictionary.ContainsKey(limb.type)) limbDictionary.Remove(limb.type); if (limbDictionary.ContainsKey(limb.type)) limbDictionary.Remove(limb.type);
//remove all joints that were attached to the removed limb //remove all joints that were attached to the removed limb
@@ -1501,7 +1515,7 @@ namespace Barotrauma
{ {
l.Remove(); l.Remove();
} }
Limbs = null; limbs = null;
} }
foreach (PhysicsBody b in collider) foreach (PhysicsBody b in collider)
@@ -1981,12 +1981,10 @@ namespace Barotrauma
public override void Remove() public override void Remove()
{ {
#if DEBUG
if (Removed) if (Removed)
{ {
DebugConsole.ThrowError("Attempting to remove an already removed character\n" + Environment.StackTrace); DebugConsole.ThrowError("Attempting to remove an already removed character\n" + Environment.StackTrace);
} }
#endif
base.Remove(); base.Remove();