Queue's in UnityScript

Hey all;

I’ve been doing some reading and C# has a class for Queue; and this I think would suit my needs very well. I’ve got a subtitles script; and the subtitles are called when a condition is met, be it a trigger or animation etc. However sometimes Script overlaps; and this isn’t ideal, hence why queueing these could be the best options.

Is there any examples of Queues in UnityScript (JS)?

What I want to do, is instead of overwriting what’s on show, just queue it, until the current is finished.

public var myStrings : String[];
public var curString : int;     
static var displaying : boolean = false;     
var myStyle : GUIStyle;
var skin : GUISkin;
var icon : Texture2D;

 function Start(){
myStrings = new String[6];
myStrings[0] = "";
myStrings[1] = "aaaaaaaaaaaaaaaaaaaaaaaa";
myStrings[2] = "bbbbbbbbg";
myStrings[3] = "rrrrrrrrrr";
myStrings[4] = "ffffffff";
myStrings[5] = "rreeeeeeeee";

}

public function ShowMe(hit : int)
    {
	      	curString = hit;
	      	displaying = true;
	      	Debug.Log("hello");
	     	yield WaitForSeconds((myStrings[curString].length / 10)+1);
       		displaying = false;
    }

Any suggestions?

Edited I’ve added some code!

A queue is just a collection of objects (like an array or List). It just stores a bunch of objects. The difference is that you can only enqueue (put something in the queue) at the end and dequeue (get something out) at the beginning of the queue. It’s a FIFO(FirstIn-FirstOut).

An example:

var myQueue = new Queue.<int>();
myQueue.Enqueue(5);
myQueue.Enqueue(7);
myQueue.Enqueue(23);
// Queue contains the elements 5,7,23

var V = myQueue.Dequeue();  // returns 5
V = myQueue.Dequeue();  // returns 7

myQueue.Enqueue(100);
// Queue contains the elements 23,100

V = myQueue.Dequeue();  // returns 23
V = myQueue.Dequeue();  // returns 100
// Queue is empty

edit
The Queue should have angle brackets after the dot “.” and the element type which should be int in my case.

There’s another function called “Peek”. Peek will return the next element that would be returned when you call Dequeue, but it doesn’t remove the element. It can be used to check what will come next without changing the queue.

You don’t have random access to the elements.

Since your “needs” are described very abstract i can’t give you any advice how to use a queue in your case. A queue can queue any kind of objects / values.

edit (after OP edit)

Ok so if i got it right you want to display those hints by calling ShowMe and each hint should be displayed a certain amount of time (calculated form the text length). Your problem is that if ShowMe is called during another one is visible you want to queue any additional hint and show them one by one.

This should do what you want:

private var textQueue = new Queue.<int>();

function Start()
{
    myStrings = new String[6];
    myStrings[0] = "";
    myStrings[1] = "aaaaaaaaaaaaaaaaaaaaaaaa";
    myStrings[2] = "bbbbbbbbg";
    myStrings[3] = "rrrrrrrrrr";
    myStrings[4] = "ffffffff";
    myStrings[5] = "rreeeeeeeee";
    ShowTextCoroutine();
}

function ShowTextCoroutine()
{
    while(true)
    {
        while (textQueue.Count > 0)
        {
            curString = textQueue.Dequeue();
            displaying = true;
            yield WaitForSeconds((myStrings[curString].length / 10)+1);
        }
        displaying = false;
        yield null;
    }
}
 
public function ShowMe(hit : int)
{
    textQueue.Enqueue(hit);
}