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.
void Update()
{
if (keyPressed && autoFillQueue.Count > 0) //keyPressed is calculated fine
{
print("Resetting Queue");
autoFillQueue.Clear(); //I only reset the queue when a letter is changed and the queue is not empty. This is not the problem.
keyPressed = false;
autoFillIndex = 0; //Just a variable to keep track of the index. Works fine.
}
if (inputPlayer.GetButtonDown("AutoFill")) //Finds the button fine and all goes well
{
if (CommandList.GetCommandList() != null)
{
var input = userConsole.text;
var commandListArray = CommandList.GetCommandList().ToArray();
if (autoFillQueue.Count == 0) //Works fine here
{
foreach (var command in commandListArray)
{
if (String.IsNullOrEmpty(input) || input.Trim() == "") //This check actually has no errors. If I do not input something, it all works fine.
{
autoFillQueue.Add(command.GetName());
continue;
}
if (command.GetName().StartsWith(input)) //THIS IS WHERE IT SEEMS TO SCREW UP
{
print(string.Format("Command Name: {0}", command.GetName())); //This logs that the only two commands I have all have passed and have been registered
autoFillQueue.Add(command.GetName());
print("Length: " + autoFillQueue.Count); //This logs the count AS 2.
}
}
}
//NOW AS IF IT WAS MAGIC, THE COUNT HAS SUDDENLY CHANGED TO 1. As logged with this print statement
print(string.Format("AutoFillIndex: {0}, Length of List: {1}", autoFillIndex, autoFillQueue.Count));
var commandName = autoFillQueue[autoFillIndex];
UpdateTextBox(commandName);
if (autoFillIndex == autoFillQueue.Count - 1)
{
autoFillIndex = 0;
}
else
{
autoFillIndex++;
}
}
}
}
If anyone can tell me what the hell is up with this, I would appreciate it.