Search Unity

Question Dictionary nested in Queue

Discussion in 'Scripting' started by Philipp_Goettler, May 18, 2023.

  1. Philipp_Goettler

    Philipp_Goettler

    Joined:
    Dec 11, 2021
    Posts:
    28
    Hey, this is my first time working with Queues. And i just wanna ask, how can i add Dictionarys into the Queue? I asigned it already, but i dont know how to add objects to it.
    Thanks for every help

    Code (CSharp):
    1. public Queue<Dictionary<UnitTypes, int>> AddUnitWithBuildingTime = new Queue<Dictionary<UnitTypes, int>>();
    2.     public Queue<GameObject> queueOfUnits = new Queue<GameObject>();
    3.     public Queue<Dictionary<GameObject, int>> Testing = new Queue<Dictionary<GameObject, int>>();
    4.  
    5.     public GameObject nextUnit;
    6.  
    7. void Start()
    8.     {
    9.         AddUnitWithBuildingTime.Enqueue(UnitTypes.archer[4]);                       // Enum + int (This has to work for my game)     (did not work)
    10.        
    11.  
    12.         Testing.Enqueue( GameObject.FindGameObjectsWithTag("Player"), 3);       // 2nd Test with a GameObject and int           (did not work)
    13.        
    14.         queueOfUnits.Enqueue(GameObject.FindGameObjectWithTag("Player"));       //  This was my first test with only Gameobjects (worked)
    15.         Debug.Log(queueOfUnits.Count);
    16.         queueOfUnits.Enqueue(GameObject.FindGameObjectWithTag("Lumberjack"));
    17.         Debug.Log(queueOfUnits.Count);
    18.         nextUnit = queueOfUnits.Dequeue();
    19.         Debug.Log(nextUnit.name);
    20.         nextUnit = queueOfUnits.Dequeue();
    21.         Debug.Log(nextUnit.name);
    22.     }
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Testing is a Queue<Dictionary<GameObject, int>>.

    Specifically it is a Queue that contains Dictionary<...>, you can't put GameObjects in it, you must put Dictionary's in it. Specifically Dictionary<GameObject, int>.

    Lets simplify this to an analogue situation:
    Code (csharp):
    1. void Foo(List<GameObject> lst)
    2. {
    3.     foreach (var go in lst) Debug.Log(go.name);
    4. }
    This function 'Foo' takes an argument of List<GameObject>... a list of gameobjects. You can't pass in a GameObject, you must past in a List of GameObjects.

    Same goes for Queue<Dictionary<GameObject, int>>.Enqueue(Dictionary<GameObject, int> item). It doesn't accept GameObjects, it accepts Dictionary's of KeyValuePair's of GameObjects and ints.

    ...

    You can add them to 'queueOfUnits', because that queue is a Queue<GameObject>, which means it's a queue of GameObjects.

    ...

    I don't know why you need a Queue of Dictionary's of GameObject ints. But if you want it... you need to create the Dictionary that the GameObject goes into and the queue them up.

    Which leads me to the age old question... "What are you attempting to do? Not what code are you writing, but the actual goals you have in mind?"
     
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    What do you want to do exactly?
    Maybe structs can help?
     
  4. Philipp_Goettler

    Philipp_Goettler

    Joined:
    Dec 11, 2021
    Posts:
    28
    At i am working on a TBS Game and got 3 different types of units. And when i want to create one (UnitTypes) of them, they should get added to the queue with the rounds they need (int).
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Create a struct like so:
    Code (csharp):
    1. public struct UnitTypeRoundsCount
    2. {
    3.     public GameObject Unit;
    4.     public int Rounds;
    5. }
    Then a queue of that:
    Code (csharp):
    1. Queue<UnitTypeRoundsCount> queueOfUnits = new();
    And then fill them with that:
    Code (csharp):
    1. var info = new UnitTypeRoundsCount() {
    2.     Unit = GameObject.FindGameObjectWithTag("Blargh"), //or how ever you want to locate the gameobejct
    3.     Rounds = 5, //or how ever you determine the rounds
    4. };
    5. queueOfUnits.Enqueue(info);
     
    Philipp_Goettler likes this.
  6. Philipp_Goettler

    Philipp_Goettler

    Joined:
    Dec 11, 2021
    Posts:
    28
    I have tested it now and i can only add a whole Dictionary, is it also possible to add only 1x object of another dictionary?
    Code (CSharp):
    1. public Queue<Dictionary<GameObject, int>> Testing = new Queue<Dictionary<GameObject, int>>();
    2.     public Dictionary<GameObject, int> testingDictionary;
    3.  
    4. void Awake() {
    5. testingDictionary = new Dictionary<GameObject, int>()
    6.         {
    7.             { archer, 3 },
    8.             { sword, 4 },
    9.             { rider, 6 }
    10.         };
    11. }
    12. void Start() {
    13. Testing.Enqueue( testingDictionary[archer]); // Does not work
    14. Testing.Enqueue( testingDictionary); // Does work but i need the other one
    15. }
     
  7. Philipp_Goettler

    Philipp_Goettler

    Joined:
    Dec 11, 2021
    Posts:
    28
    Ok i will test that one thanks.