- Home /
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!
Queue is not a C# thing, it's a .NET thing. Unityscript uses .NET (or technically $$anonymous$$ono). Therefore it has Queue.
Answer by Bunny83 · Jan 27, 2013 at 01:24 PM
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);
}
[$$anonymous$$eta] Hmm, is there any good reason why empty lines in a code block are stripped? Now we have line-numbers, that's nice, but if empty lines are stripped away it makes no sense at all.
I inserted some empty lines to make it more readable, now it's just glued together.
edit ohh and it seems that generic parameters are also stripped again .............
Why updates are always one step forward and 3 back?
edit: It's funny HT$$anonymous$$L characters are now interpreted in code blocks. I temporarily fixed my post with "lt" and "gt"
Hey bunny, I've added my script above, as maybe that would help you in my sense =/
Needless to say you've provided a fantastic answer; i will go over it thoroughly, and write back!
@BobbleHead: I've added another example how you could use a queue. $$anonymous$$eep in $$anonymous$$d that the generic Queue as well as the generic List, Stack, Dictionary are in the System.Collections.Generic namespace, so you probably need to add:
import System.Collections.Generic;
at the top of your script. I don't use UnityScript, so i'm not sure if the compiler includes the namespace by default or not.
Hey Bunny, have copied that code over (which i thank you for) but it seems thints show for a matter of frames, so it's like super quick. I tried dividing and multiplying text length, but this only helped slightly.
I am unsure how to progress; but am going to try to learn more this afternoon.
I think it depends from which side you look at the queue ;) How it's layed out in memory doesn't really matter. It's a black box which has an input and an output. Everything you put in comes out in the same order.
edit I just tried it and yes, you're right, the internal layout of the queue is that you add at the end of the queue. So Enqueue of the Queue works like Add of the List.
Just because i was testing this i checked the Stack as well ;) The Stack works the other way round. New items are inserted at the beginning and of course removed from the beginning. Since both, Stack and Queue are usually implemented as LinkedList there is no real beginning or end. The operation of adding or removing an element would be equal for the "front" or "back".
Your answer
Follow this Question
Related Questions
Queue> not working? 4 Answers
Kinect Dance Game Unity3d 2 Answers
(HELP) Volume Ray Marching rendered always on top of the other objects 2 Answers
String taken from a JSON file is not being Queued 1 Answer
Checking if RPC queue is empty? 0 Answers