Creating a buffer!

Hi.
I would like to be able to create a buffer that is able to take in 60 entries in this form :

ID (int) : KEY (String), Value (bool).

It should take 1 new entry each frame, and it should never get over 60 max entries , instead it should remove the old ones and keep adding new ones at the end of the buffer.

Any help is greatly appreciated.

not quite sure what you mean by buffer but you should store the entries in a queue with the max size set to 60

–

Okay , any example you can give? Would be great!

–

using UnityEngine; using System.Collections; public class YourClass : MonoBehaviour { public Object[] entry; public queue buffer; int maxSize = 60; public void AddStuff(int ID, String Key, bool Value) { entry.Add(ID, Key, Value); if (buffer.Count > 60) { buffer.Dequeue(); } buffer.Enqueue(entry); } } Its not exactly that but its something like that. you might have to use a seperate class or method for the entry arrays than from the buffer queue

–

Thank you very much ! You could post it as an answer , I would like to vote it.

–

2 Answers

2

You can use the List Generic class.
Now first we need to create a instance of the List class to do that wee need to add using System.Collections; to the top of out CS/JS File(UnityScript uses import instead of using).

after that we declare out member wise Stringbuilder variable.

//60 can be any number, its the capacity of your buffer
List<MyStruct> buffer = new List<MyStruct>();

Next we’ll do out update function. Since string builder will Grow when we exceed 60, we need to manually remove the first element, before it hits 61.

void Update() {

//Length is the current amount of Objects, Capacity is the max
if(buffer.Length == 60) {

//0 is start index, 1 is the range length(which is one)
buffer.Remove(0,1);
}

//Instert your new data, Struct instance is an instance of your data
buffer.Insert(buffer.Length, myStructInstance)

}

Now your probably noticed i uses “MyStruct” all over the place. well that is the Object that contains your “data”.

the struct should look a little like this.

 public struct BufferStruct {

   int ID;
   string KEY;
   bool Value;
    
    public BufferStruct(int id, string key, bool value) {
        this.ID = id;
        this.KEY = key
        this.Value = value;

        }

}

Hope it Helps

Thank you very much, very well explained!

–

your welcome, I like ViceVersa's "Comment" Answer though (i didn't see it when writing this one) it does the same thing, but you don't have to mess around with indexes, ranges or generics.

–

Yes his method is a bit clearer.

–

Using a Queue instead of a List would be better imho.

–

Yes, but I actually didn't even know Queue existed until now. hence this Answer

–

using UnityEngine;
using System.Collections;

public class YourClass : MonoBehaviour {

public Object[] entry;
public queue buffer;
int maxSize = 60;
public  void AddStuff(int ID, String Key, bool Value)
{
 entry.Add(ID, Key, Value);
if (buffer.Count > 60)
{
buffer.Dequeue();
}

buffer.Enqueue(entry);

}
}

Its not exactly that but its something like that. you might have to use a seperate class or method for the entry arrays than from the buffer queue