Dictionary nested in Queue

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

public Queue<Dictionary<UnitTypes, int>> AddUnitWithBuildingTime = new Queue<Dictionary<UnitTypes, int>>();
    public Queue<GameObject> queueOfUnits = new Queue<GameObject>();
    public Queue<Dictionary<GameObject, int>> Testing = new Queue<Dictionary<GameObject, int>>();

    public GameObject nextUnit;

void Start()
    {
        AddUnitWithBuildingTime.Enqueue(UnitTypes.archer[4]);                       // Enum + int (This has to work for my game)     (did not work)


        Testing.Enqueue( GameObject.FindGameObjectsWithTag("Player"), 3);       // 2nd Test with a GameObject and int           (did not work)

        queueOfUnits.Enqueue(GameObject.FindGameObjectWithTag("Player"));       //  This was my first test with only Gameobjects (worked)
        Debug.Log(queueOfUnits.Count);
        queueOfUnits.Enqueue(GameObject.FindGameObjectWithTag("Lumberjack"));
        Debug.Log(queueOfUnits.Count);
        nextUnit = queueOfUnits.Dequeue();
        Debug.Log(nextUnit.name);
        nextUnit = queueOfUnits.Dequeue();
        Debug.Log(nextUnit.name);
    }

Testing is a Queue>.

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

Lets simplify this to an analogue situation:

void Foo(List<GameObject> lst)
{
    foreach (var go in lst) Debug.Log(go.name);
}

This function 'Foo' takes an argument of List... a list of gameobjects. You can't pass in a GameObject, you must past in a List of GameObjects.

Same goes for Queue>.Enqueue(Dictionary 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, 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?"

What do you want to do exactly?
Maybe structs can help?

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).

Create a struct like so:

public struct UnitTypeRoundsCount
{
    public GameObject Unit;
    public int Rounds;
}

Then a queue of that:

Queue<UnitTypeRoundsCount> queueOfUnits = new();

And then fill them with that:

var info = new UnitTypeRoundsCount() {
    Unit = GameObject.FindGameObjectWithTag("Blargh"), //or how ever you want to locate the gameobejct
    Rounds = 5, //or how ever you determine the rounds
};
queueOfUnits.Enqueue(info);
1 Like

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?

public Queue<Dictionary<GameObject, int>> Testing = new Queue<Dictionary<GameObject, int>>();
    public Dictionary<GameObject, int> testingDictionary;

void Awake() {
testingDictionary = new Dictionary<GameObject, int>()
        {
            { archer, 3 },
            { sword, 4 },
            { rider, 6 }
        };
}
void Start() {
Testing.Enqueue( testingDictionary[archer]); // Does not work
Testing.Enqueue( testingDictionary); // Does work but i need the other one
}

Ok i will test that one thanks.