- Home /
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;
}
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.
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
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.
Yeah, that's basically what I'm seeing too. Your code is far more elegant, though, and faster to boot.