RTS C# Queue question

Hey there,

So I’m trying to set up a queue that when you say press space it adds the game object to a queue and then after 5 seconds it spawns and each press adds it to the queue. However, my question is how is the best way to set up the actual queue part. I know it would be with the queue system but I am clueless on how to actually set up properly to get it to work. So how does one set that up?

A full tutorial on the generic Queue if C# is beyond the scope of a post here, and you’ll benefit from searching Google for that as a general C# task.

However, a quick start is workable. First, don’t be daunted by the sheer size of the documentation on System.Queue. Much of the interface isn’t much used.

First, you’ll probably need to consider how the elements are stored. A generic queue expects a common type, so if you have various types of objects, you’ll need to fashion a common means of referencing them. A common means Unity already uses is transform, because it refers to the gameObject and from there you can get components of anything, even if the specific objects differ. You could also create a class representing the objects you’re collecting, which have with them references to the various object’s transform, script(s) or other materials of pertinence for your case.

You create a queue with something like

Queue<type> queuedstuff = new Queue<type>();

Where type is that class you use as a common reference, possibly transform or your own class that holds the transform (and other data), or whatever.

You add to the queue with something like

    queuedstuff.Enqueue( item );

where “item” is your newly created class.

You remove items from the queue with something like

    item = queuedstuff.Dequeue()

That pulls the oldest item out of the queue.

That’s the basics of this.

Now, the notion of doing something 5 seconds later is a bit beyond what the queue does. You may need to use queuedstuff.Peek() to check the last object for an expiration time during Update, acting only if the duration has passed.