Author Topic: Fog of War Thread Error  (Read 7463 times)

garside

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Fog of War Thread Error
« on: February 26, 2015, 08:51:59 AM »
So using the FoW system, I ran into an issue where instantiating new prefabs with revealers on them would sometimes cause the entire FoW system to break and stop responding.

I managed to trace the issue down to a buffer not having the proper data at the time of updating which caused an error which silently killed the thread.

In the FOWSystem.cs file I made the following changes which seem to have made it resilient to this problem. The try/catch allows the thread to keep working when the instantiating buffer problem happens, and it seems to have kept things alive for me? Posting it here in case anyone else has this issue!

Note, the "Thread is alive" check in the late update function is a last-ditch effort to restart the system if it dies for another reason. It wasn't ever fired once the try/catch was put in the ThreadUpdate method.

  1. @@ -284,6 +284,13 @@ void LateUpdate ()
  2.                 Shader.SetGlobalVector("_FOWParams", p);
  3.                 Shader.SetGlobalTexture("_FOWTex0", mTexture0);
  4.                 Shader.SetGlobalTexture("_FOWTex1", mTexture1);
  5.  
  6.                 if (!mThread.IsAlive)
  7.                 {
  8.                         Debug.LogError("FOWThread Dead; Restarting");
  9.                         mThread = new Thread(ThreadUpdate);
  10.                         mThread.Start();
  11.                 }      
  12.         }
  13.  
  14.         /// <summary>
  15. @@ -300,7 +307,14 @@ void ThreadUpdate ()
  16.                         {
  17.                                 sw.Reset();
  18.                                 sw.Start();
  19.                                 UpdateBuffer();
  20.                                 try
  21.                                 {
  22.                                         UpdateBuffer();
  23.                                 }
  24.                                 catch (System.Exception e)
  25.                                 {
  26.                                         Debug.LogWarning("FOWThreadException: " + e.ToString());
  27.                                 }
  28.                                 sw.Stop();
  29.                                 if (debug) Debug.Log(sw.ElapsedMilliseconds);
  30.                                 mElapsed = 0.001f * (float)sw.ElapsedMilliseconds;
  31.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fog of War Thread Error
« Reply #1 on: February 27, 2015, 05:03:11 AM »
Buffer not having proper data? Which buffer? FOWRevealer adds itself to the list of revealers only after locking the array, implying that the FOWSystem won't be able to access it at the time.

garside

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Fog of War Thread Error
« Reply #2 on: March 03, 2015, 07:44:18 AM »
Sorry I didn't write it down. I can probably reproduce it easily enough?

I was looking through the code and saw that it was adding itself with locking the array so I didn't think that was going to be the problem.

It was something to do with r.cachedBuffer being uninitialized or something IIRC?

The error was transient and hard to reproduce.

Basically, there was some underlying condition (maybe a race condition given the threading?) which after instantiating a gameobject with a FOWRevelaer on it, there was like a 10% chance the entire FOW system would stop responding.

After unsuccessfully poking around other parts of the code, I managed to get narrow it down to an error happening in that UpdateBuffer() call.

I think the cacheBuffer was uninitialized in the RevealIntoCache method. However, this was a temporary thing (which is why I thought race condition), as simply not breaking on the error causes smooth operation once the error gets swallowed.

I just tried for a bit to reproduce it but of course was unable. I had the stack trace in a text file on my notepad but I moved my computer since then and had closed it absentmindedly. :|

garside

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Fog of War Thread Error
« Reply #3 on: March 03, 2015, 03:30:04 PM »
Okay so while working on it today, whatever causes it to happen fired, and it tripped up my warning logger. Here's the stack trace of the error which would have otherwise killed the FOWSystem:

  1. FOWThreadException: System.IndexOutOfRangeException: Array index is out of range.
  2.   at FOWSystem.RevealIntoCache (.Revealer r, Single worldToTex) [0x001eb] in C:\Users\Eric\Code\ConjurersQuest\Assets\Scripts\Vendor\FogOfWar\FOWSystem.cs:701
  3.   at FOWSystem.RevealUsingCache (.Revealer r, Single worldToTex) [0x0000b] in C:\Users\Eric\Code\ConjurersQuest\Assets\Scripts\Vendor\FogOfWar\FOWSystem.cs:624
  4.   at FOWSystem.UpdateBuffer () [0x001dd] in C:\Users\Eric\Code\ConjurersQuest\Assets\Scripts\Vendor\FogOfWar\FOWSystem.cs:493
  5.   at FOWSystem.ThreadUpdate () [0x00023] in C:\Users\Eric\Code\ConjurersQuest\Assets\Scripts\Vendor\FogOfWar\FOWSystem.cs:312
  6.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fog of War Thread Error
« Reply #4 on: March 03, 2015, 06:04:11 PM »
Sounds like you need to update your FOW package. Line 701 of FOWSystem for me is an "if" statement:
  1. if (x > 0 && x < limit)

garside

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Fog of War Thread Error
« Reply #5 on: March 03, 2015, 06:43:49 PM »
Sorry that's probably from my modifications.

Here's my copy of the file.

  1.  
  2. 699                                             if (dist < minRange || (cx == x && cy == y))
  3. 700                                             {
  4. 701                                                     r.cachedBuffer[(x - xmin) + (y - ymin) * size] = true;
  5. 702                                             }
  6.  
  7.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fog of War Thread Error
« Reply #6 on: March 05, 2015, 10:43:27 PM »
Unfortunately I can't have a look at it right now as I am at PAX East. I should be able to do it around Tuesday when I return home.

garside

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Fog of War Thread Error
« Reply #7 on: March 06, 2015, 04:13:10 AM »
No rush or worries! More just an FYI than an open issue for me. :)

Have a nice time!