Search Unity

Programming Issue Question

Discussion in 'Getting Started' started by CreedGameStudios, Oct 31, 2016.

  1. CreedGameStudios

    CreedGameStudios

    Joined:
    Aug 26, 2016
    Posts:
    44
    I ran into a weird problem, not sure if I missed something or if there's an interaction issue. There's a List of specials to be processed, but sometimes those create new specials that get added in the other functions. I wanted to add a slight time delay between those, so I put in a boolean and used an Invoke statement.

    The problem is, when I switch to the IF statement that's commented out, it causes an endless loop. I've isolated the issue to this group of commands/functions.

    Code (CSharp):
    1.     public List<Special> specials = new List<Special> ();
    2.     private bool specCD = true;
    3.  
    4.     void Process ()  {
    5.         RemoveMatches ();
    6.         do  {
    7.             RemoveSpecials ();
    8.         } while (specials.Count != 0);
    9.         Invoke ("ReloadMap", 0.45f);
    10.     }
    11.  
    12.     void RemoveSpecials ()  {
    13.         int sc = specials.Count;
    14.  
    15.         //if (sc != 0 && specCD == true) {  
    16.         if (sc != 0) {
    17.                 //Process the list of pending special tile effects
    18.             for (int e = 0; e < sc; e++) {
    19.                 if (specials[e].z == 1)  Stuff;
    20.                 if (specials[e].z == 2)  OtherStuff;
    21.             }
    22.             specials.RemoveRange(0 , sc);
    23.                //Clear out just the specials from when this loop started
    24.             specCD = false;
    25.                //Start the cooldown delay
    26.             Invoke ("SpecialCool", 0.5f);
    27.         }
    28.     }
    29.  
    30.     void SpecialCool()  {
    31.         specCD = true;
    32.     }
    (I'm sure there's a cleaner way to process delays using Coroutines or something, but I'm not familiar with those yet, and the Invoke delays have been working so far, especially since this is a linear series of functions.)