Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Wrap VesselFlare Update with try/catch in case
Browse files Browse the repository at this point in the history
a vessel disappears and we don't see the callback for some reason.
  • Loading branch information
MOARdV committed Jun 23, 2016
1 parent 3a13776 commit 4ffca71
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions Source-Code/FlareDraw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,39 @@ class VesselFlare

public void Update(Vector3d camPos, float camFOV)
{
Vector3d targetVectorToCam = camPos - referenceShip.transform.position;
float targetDist = (float)Vector3d.Distance(referenceShip.transform.position, camPos);
bool activeSelf = flareMesh.activeSelf;
if (targetDist > 750000.0f && activeSelf)
try
{
flareMesh.SetActive(false);
activeSelf = false;
}
else if (targetDist < 750000.0f && !activeSelf)
{
flareMesh.SetActive(true);
activeSelf = true;
}
Vector3d targetVectorToCam = camPos - referenceShip.transform.position;
float targetDist = (float)Vector3d.Distance(referenceShip.transform.position, camPos);
bool activeSelf = flareMesh.activeSelf;
if (targetDist > 750000.0f && activeSelf)
{
flareMesh.SetActive(false);
activeSelf = false;
}
else if (targetDist < 750000.0f && !activeSelf)
{
flareMesh.SetActive(true);
activeSelf = true;
}

if (activeSelf)
{
brightness = Mathf.Log10(luminosity) * (1.0f - Mathf.Pow(targetDist / 750000.0f, 1.25f));
if (activeSelf)
{
brightness = Mathf.Log10(luminosity) * (1.0f - Mathf.Pow(targetDist / 750000.0f, 1.25f));

flareMesh.transform.position = camPos - targetDist * targetVectorToCam.normalized;
flareMesh.transform.LookAt(camPos);
float resizeFactor = (0.002f * targetDist * brightness * (0.7f + .99f * camFOV) / 70.0f) * DistantObjectSettings.DistantFlare.flareSize;
flareMesh.transform.position = camPos - targetDist * targetVectorToCam.normalized;
flareMesh.transform.LookAt(camPos);
float resizeFactor = (0.002f * targetDist * brightness * (0.7f + .99f * camFOV) / 70.0f) * DistantObjectSettings.DistantFlare.flareSize;

flareMesh.transform.localScale = new Vector3(resizeFactor, resizeFactor, resizeFactor);
//Debug.Log(string.Format("Resizing vessel flare {0} to {1} - brightness {2}, luminosity {3}", referenceShip.vesselName, resizeFactor, brightness, luminosity));
flareMesh.transform.localScale = new Vector3(resizeFactor, resizeFactor, resizeFactor);
//Debug.Log(string.Format("Resizing vessel flare {0} to {1} - brightness {2}, luminosity {3}", referenceShip.vesselName, resizeFactor, brightness, luminosity));
}
}
catch
{
// If anything went whack, let's disable ourselves
flareMesh.SetActive(false);
referenceShip = null;
}
}

Expand Down Expand Up @@ -337,11 +346,11 @@ private void GenerateVesselFlares()
#endif
// See if there are vessels that need to be removed from our live
// list
foreach (Vessel v in vesselFlares.Keys)
foreach (var v in vesselFlares)
{
if (v.orbit.referenceBody != FlightGlobals.ActiveVessel.orbit.referenceBody || v.loaded == true || !situations.Contains(v.situation))
if (v.Key.orbit.referenceBody != FlightGlobals.ActiveVessel.orbit.referenceBody || v.Key.loaded == true || !situations.Contains(v.Key.situation) || v.Value.referenceShip == null)
{
deadVessels.Add(v);
deadVessels.Add(v.Key);
}
}
#if SHOW_FIXEDUPDATE_TIMING
Expand Down

0 comments on commit 4ffca71

Please sign in to comment.