Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by BobbleHead · Jan 27, 2013 at 12:58 PM · queue

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jan 27, 2013 at 07:48 PM 0
Share

Queue is not a C# thing, it's a .NET thing. Unityscript uses .NET (or technically $$anonymous$$ono). Therefore it has Queue.

1 Reply

· Add your reply
  • Sort: 
avatar image
7
Best Answer

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);
 }

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 27, 2013 at 01:28 PM 1
Share

[$$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"

avatar image BobbleHead · Jan 27, 2013 at 01:29 PM 0
Share

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!

avatar image Bunny83 · Jan 27, 2013 at 01:50 PM 0
Share

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

avatar image BobbleHead · Jan 27, 2013 at 03:10 PM 0
Share

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.

avatar image Bunny83 · Jan 27, 2013 at 07:42 PM 1
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges