Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by DevMerlin · Apr 22, 2016 at 08:49 PM · c#listqueue

C# - Is it possible to sync a Queue to a List, or is there a better option?

I have a bit of a interesting problem in that I'm using a Queue to manage a endless runner pool. However, I just realized that the items in the queue can't be accessed and selected for quick modification.

Temporarily, I've also added a List, but without being constantly updated, that list does not stay synced to the positions of the object - for instance, 5 in the List could be at the very back of the track, while at the start it would reference the 5th track piece on screen.

My major goal is to do things like mark the last track piece that a player actively touched before they died, or modify a track in a event system. Having a list also helps keep track of spawning positions.

What option would be best for me to use here?

Comment
Add comment · Show 2
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 jgodfrey · Apr 22, 2016 at 09:37 PM 0
Share

It's not clear to me exactly what you're trying to do with the Queue. What is the content of the objects that are in the Queue? I'd guess that you only want/need a single collection type (List, Queue, ??) ins$$anonymous$$d of trying to sync multiple collections, though it's hard to tell without more info.

Can you provide more details?

avatar image DevMerlin jgodfrey · Apr 23, 2016 at 02:44 AM 0
Share

I'm attempting to view the data in the queue as it exists in an array or list so that I can access the individual pieces as they currently are in queue order. However, I did discover that Queue has "ToArray()". I am sort of wondering if iterating that each time the list is modified would be too expensive or not.

$$anonymous$$y problem is that right now, outside the queue each piece exists independently without a reference to it in the code.

1 Reply

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

Answer by FWCorey · Apr 23, 2016 at 02:56 AM

Your best bet if you are truly concerned about performance is to try several methods, profiling each until you find the one that works best for your particular use case. If your queue has a known maximum size you might want to use an array with pointers and create a circular queue that way. That would be the fastest, but it's expensive to expand the maximum size and if you need access to more than basic functionality can be a little advanced to implement as a generic IEnumerable class.

Comment
Add comment · Show 3 · 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 DevMerlin · Apr 23, 2016 at 03:03 AM 0
Share

Thankfully the queue is limited to a hundred items. It should never expand beyond that, the elements inside just get recycled and re-positioned.

avatar image FWCorey · Apr 23, 2016 at 03:24 AM 0
Share

Then a circular Array queue will work best for you. Just create a class with an array of your items and a few int pointers

int _topOfQueue index pointer for the top of the queue int _bottomOfQueue index pointer for the bottom of the queue int Count {get; private set;} property for the count of items

an Enqueue method that increments _topOfQueue with wraparound sets a new item to the array element at index _topOfQueue and increments Count

a Dequeue method to check that Count is greater than 0 and throw an error or return the item at array index _bottomOfQueue and then increment _bottomOfQueue and decrease Count

a Peek method to check that Count is greater than 0 and throw an error or return the item at array index _bottomOfQueue. Dequeue can call this before updating the pointers but despite the code duplication, it would shorten the call stack when debugging to keep them separate.

a Clear method to simply set _topOfQueue, _bottomOfQueue and Count to zero

avatar image FWCorey · Apr 23, 2016 at 03:28 AM 0
Share

If there is ANY chance the queue size would change you can add a check in Enqueue to make sure _topOfQueue never increments to the same value as _bottomOfQueue with a Count greater than 0 and resize and reorder the array if it does. $$anonymous$$aybe with a Debug.Warning message so you can change the initial size to prevent it happening again

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

134 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Queueing methods and calling them at specific times 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

object assigns but does not show when added to list, or any other var 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