Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Queue problem

Discussion in 'Scripting' started by unity_1236alman, Jul 30, 2021.

  1. unity_1236alman

    unity_1236alman

    Joined:
    Mar 4, 2019
    Posts:
    66
    Basically i have a Queue (todoActions) and I run this code. It works as i want but the Debug.Log is printing always the initial count of todoActions - 1. Can anyone explain me why?
    Code (CSharp):
    1.  
    2. void Play()
    3. {
    4. bool executedAction;
    5.         do
    6.         {
    7.             action todoAction = todoActions.Peek();
    8.             executedAction = false;
    9.  
    10.             if (todoAction.executionTick == actualTick)
    11.             {
    12.                 ExecuteAction(todoAction); //This does not interact with the queue
    13.                 executedAction = true;
    14.                 todoActions.Dequeue();
    15.            
    16.                 Debug.Log(todoActions.Count);
    17.             }
    18.  
    19.         } while (executedAction);
    20. }
     
  2. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,115
    Maybe the if condition is only true at first run. Try removing it to test if that's the reason.
     
  3. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    324
    Your code smells wrong, queue<T>.Peek throws InvalidOperationException when empty, you should test count before Peek or Dequeue. You should get an exception with below code, if not something is really wrong.

    Exception here
    Code (CSharp):
    1.     private void Play()
    2.     {
    3.         todoActions.Enqueue(new action());
    4.         todoActions.Enqueue(new action());
    5.         todoActions.Enqueue(new action());
    6.         todoActions.Enqueue(new action());
    7.      
    8.         bool executedAction;
    9.         do
    10.         {
    11.             var todoAction = todoActions.Peek();
    12.          
    13.             todoActions.Dequeue();
    14.          
    15.             Debug.Log(todoActions.Count);
    16.  
    17.         } while (true);
    18.     }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Um, because you call .Dequeue() in the line immediately preceeding the Debug.Log() line??

    Always start with the docs for Queue<T>() and the details of its operation, including the C# sample code provided, which looks something along the lines of:

    Code (csharp):
    1. if (myQueue.Count > 0)
    2. {
    3.    var foo = myQueue.Dequeue();
    4.    // TODO: do great things with foo here
    5. }
     
  5. unity_1236alman

    unity_1236alman

    Joined:
    Mar 4, 2019
    Posts:
    66
    I mean is printing the same number (queue count - 1) at every cycle, and the cycle is always executed 50+ times. Also, I can't start with dequeue because if the condition that i need is false, i don't remove the peeked item from the queue
     
  6. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    Need more context

    Make the most basic implementation with all these elements included and ready to run.
    Add a several actions, make them debug.logs.

    If you're still having the issue with that basic implementation, then post the code.