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
0
Question by Mikeywalsh · Oct 04, 2013 at 08:49 AM · c#arrayfor-loop

C# Killfeed issues

I'm having an issue creating a kill feed for my unity game, it's probably something silly but i just can't find what the problem is. I have a procedure that updates the kill feed, but every time i call it, the feed array creates the same string in position 0 & 1, then 2 & 3 etc, it's supposed to shift everything in the array along by 1 space then write to position 0, i don't know why this is happening.

     void updateFeed(string killer, string killed) {
         for(int c = 0; c < 4; c++)
         {
                 feed[4 - c] = feed[3 - c];
         }
             feed[0] = killer + " has killed " + killed;
         }
Comment
Add comment · Show 5
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 Hoeloe · Oct 04, 2013 at 09:05 AM 0
Share

The code you've posted should work. Where is feed defined, and where is the method called?

I could understand it giving the result you talked about if the assignment to feed[0] were before the loop, but not after.

avatar image Jamora · Oct 04, 2013 at 09:24 AM 1
Share

Because you're implementing a queue, I would suggest using a linked list:

 LinkedList< string > messages = new LinkedList< string >();
 
 void updateFeed(string killer, string killed) {
     if(messages.Count > max$$anonymous$$essageCount){ //I think 4 in this case
         messages.RemoveLast();
     }
     messages.AddFirst(/*the message*/);
 }

Not only is it much more obvious what you're doing, but the overhead of iterating the array grows as your max messagecount increases.

Additionally, depending on how often you have to add to this list, it might be wise to use StringBuilder ins$$anonymous$$d of concatenating strings like that

avatar image Hoeloe · Oct 04, 2013 at 09:27 AM 0
Share

A Queue is definitely the way to go. Use this solution.

avatar image Jamora · Oct 04, 2013 at 09:30 AM 0
Share

I don't think it's the solution. The code posted by the OP seems to work (I think). $$anonymous$$y guess is that updateFeed gets called twice. I'm waiting for the code you asked for, Hoeloe.

avatar image Hoeloe · Oct 04, 2013 at 09:33 AM 0
Share

Yeah, that's basically what I'm seeing too. Your code is far more elegant, though, and faster to boot.

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

How to deactivate all GameObject in a array, except last one 4 Answers

Error CS0029 Help? (Screenshot of Exact Error) 1 Answer

C# For loop in button to set gameObjects in array to active 1 Answer

Array Overflow Problem 1 Answer

How to save a 2d-array in C# 2 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