Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

List Removes Element For NO REASON

Discussion in 'Scripting' started by MrHappyKiller, Aug 2, 2017.

  1. MrHappyKiller

    MrHappyKiller

    Joined:
    Feb 8, 2016
    Posts:
    29
    This has just boggled my mind entirely. I'm working on a simple autofill function for my dev console. I have a list that is populated by command names that start with the current user input. Those get checked and are added to the autoFillQueue list of strings that gets iterated over when the tab key is pressed. This was working fine until I discovered this bug. When I type in a few letters, it populates my list then REMOVES EVERYTHING BUT ONE STRING. Please take a look at the comments I made in the code below so you understand exactly what is happening.

    Code (CSharp):
    1.  
    2.  
    3.     void Update()
    4.     {
    5.         if (keyPressed && autoFillQueue.Count > 0) //keyPressed is calculated fine
    6.         {
    7.             print("Resetting Queue");
    8.  
    9.             autoFillQueue.Clear(); //I only reset the queue when a letter is changed and the queue is not empty. This is not the problem.
    10.  
    11.             keyPressed = false;
    12.             autoFillIndex = 0; //Just a variable to keep track of the index. Works fine.
    13.         }
    14.  
    15.         if (inputPlayer.GetButtonDown("AutoFill")) //Finds the button fine and all goes well
    16.         {
    17.             if (CommandList.GetCommandList() != null)
    18.             {
    19.                 var input = userConsole.text;
    20.                 var commandListArray = CommandList.GetCommandList().ToArray();
    21.  
    22.                 if (autoFillQueue.Count == 0) //Works fine here
    23.                 {
    24.                     foreach (var command in commandListArray)
    25.                     {
    26.                         if (String.IsNullOrEmpty(input) || input.Trim() == "") //This check actually has no errors. If I do not input something, it all works fine.
    27.                         {
    28.                             autoFillQueue.Add(command.GetName());
    29.  
    30.                             continue;
    31.                         }
    32.  
    33.                         if (command.GetName().StartsWith(input)) //THIS IS WHERE IT SEEMS TO SCREW UP
    34.                         {
    35.                             print(string.Format("Command Name: {0}", command.GetName())); //This logs that the only two commands I have all have passed and have been registered
    36.  
    37.                             autoFillQueue.Add(command.GetName());
    38.  
    39.                             print("Length: " + autoFillQueue.Count); //This logs the count AS 2.
    40.                         }
    41.                     }
    42.                 }
    43.  
    44.                 //NOW AS IF IT WAS MAGIC, THE COUNT HAS SUDDENLY CHANGED TO 1. As logged with this print statement
    45.  
    46.                 print(string.Format("AutoFillIndex: {0}, Length of List: {1}", autoFillIndex, autoFillQueue.Count));
    47.  
    48.                 var commandName = autoFillQueue[autoFillIndex];
    49.  
    50.                 UpdateTextBox(commandName);
    51.  
    52.                 if (autoFillIndex == autoFillQueue.Count - 1)
    53.                 {
    54.                     autoFillIndex = 0;
    55.                 }
    56.                 else
    57.                 {
    58.                     autoFillIndex++;
    59.                 }
    60.             }
    61.         }
    62.     }
    If anyone can tell me what the hell is up with this, I would appreciate it.
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,412
    Hmm, looked over that code.. I'm not 100% convinced that the two print lines are from the same call to Update()..

    There is no code between those two print statement that could remove an item from that list. As you say, it's like magic..

    In my experience, every time "magic" is happening the results I think I'm seeing are not the real results and there's something else at play (or it's multi-threaded and someone's been silly, but I expect this is not multi-threaded).

    Could you add a print statement after every line like:

    Code (csharp):
    1. void Update()
    2.     {
    3.         if (keyPressed && autoFillQueue.Count > 0) //keyPressed is calculated fine
    4.         {
    5.             print("Resetting Queue");
    6.             print("** 1 **");
    7.             autoFillQueue.Clear(); //I only reset the queue when a letter is changed and the queue is not empty. This is not the problem.
    8.             print("** 2 **");
    9.             keyPressed = false;
    10.             print("** 3 **");
    11.             autoFillIndex = 0; //Just a variable to keep track of the index. Works fine.
    12.             print("** 4 **");
    13.         }
    14.  
    15.         if (inputPlayer.GetButtonDown("AutoFill")) //Finds the button fine and all goes well
    16.         {        
    17.             print("** 5 **");
    18.  
    19.             if (CommandList.GetCommandList() != null)
    20.             {
    21.                 print("** 6 **");
    22.                 var input = userConsole.text;
    23.                 print("** 7 **");
    24.                 var commandListArray = CommandList.GetCommandList().ToArray();
    25.                 print("** 8 **");
    26.                 if (autoFillQueue.Count == 0) //Works fine here
    27.                 {
    28.                     print("** 9 **");
    29.                     foreach (var command in commandListArray)
    30.                     {
    31.                         print("** 10 **");
    32.                         if (String.IsNullOrEmpty(input) || input.Trim() == "") //This check actually has no errors. If I do not input something, it all works fine.
    33.                         {
    34.                             print("** 10.1 **");
    35.                             autoFillQueue.Add(command.GetName());
    36.                             print("** 10.2 **");
    37.                             continue;
    38.                         }
    39.  
    40.                         print("** 10.3 **");
    41.                         if (command.GetName().StartsWith(input)) //THIS IS WHERE IT SEEMS TO SCREW UP
    42.                         {
    43.                             print("** 10.4 **");
    44.                             print(string.Format("Command Name: {0}", command.GetName())); //This logs that the only two commands I have all have passed and have been registered
    45.                             print("** 10.5 **");
    46.                             autoFillQueue.Add(command.GetName());
    47.                             print("** 10.6 **");
    48.                             print("Length: " + autoFillQueue.Count); //This logs the count AS 2.
    49.                             print("** 10.7 **");
    50.                         }
    51.                     }
    52.                 }
    53.                 print("** 11 **");
    54.                 //NOW AS IF IT WAS MAGIC, THE COUNT HAS SUDDENLY CHANGED TO 1. As logged with this print statement
    55.  
    56.                 print(string.Format("AutoFillIndex: {0}, Length of List: {1}", autoFillIndex, autoFillQueue.Count));
    57.  
    58.                 var commandName = autoFillQueue[autoFillIndex];
    59.                 print("** 12 **");
    60.                 UpdateTextBox(commandName);
    61.                 print("** 13 **");
    62.                 if (autoFillIndex == autoFillQueue.Count - 1)
    63.                 {
    64.                     print("** 14 **");
    65.                     autoFillIndex = 0;
    66.                 }
    67.                 else
    68.                 {
    69.                     print("** 15 **");
    70.                     autoFillIndex++;
    71.                 }
    72.             }
    73.         }
    74.     }
    and paste the output? I suspect we're not going to see direct progression from 10.7 to 11 in this error case.
     
  3. MrHappyKiller

    MrHappyKiller

    Joined:
    Feb 8, 2016
    Posts:
    29
    I did just as you said. And in doing so, I realized that I was setting keyPressed to try when I was entering the characters before attempting to autofill. This caused the variable to be true when the list was populated (not what I wanted) which resulted in it resetting itself. Little things like this are what keep me stuck on something for hours. Thanks though :p
     
    larku likes this.
unityunity