- Home /
Storing many array values in a variable
Quick question. Is it possible to store multiple array values in a variable without having to specify each and every array value itself?
IE. from array[0] to array[30] = var X
I ask, because I am attempting to create a (simple) spectrum analyzer to manipulate my game objects via music.
From what I have researched I should use AudioListener.GetSpectrumData() -- for the first int within that function I used 1024, splitting the spectrum into 1024 parts (I presume). I know I could go as low as 64, but I feel like the spectrum split wouldn't really give me a nice visual manipulation. That and writing out 0 to 64 still seems like a pain in the ass. :P
Thank you kindly!
Answer by flamy · Mar 31, 2014 at 12:00 PM
Actually this is pretty simple while using List and Linq
do something like this,
List<int> temp;
temp = new List<int>();
temp.AddRange(Enumerable.Repeat(1,64));
temp.AddRange(Enumerable.Repeat(3,64));
this will fill the list with 128 items first 64 will be 1 and next 64 will be 3.... this is one of the easiest available solutions.
if there are issues using list and the number of entries will always be the same ( i.e., 64), then you can just save the value once to an array like this
int[] temp;
temp = new int[2];
temp[0] = 1;
temp[1] = 3;
and while retriving if u need 100th value in the array (assuming that there are 128 entries jus like the list example.) you can do something like
temp[(int)Mathf.Floor(100/64)];
similarly for any array index values.
if the array size is varying and not 64 always, then you would have to manually write an function that fills the values.
public void FillMyArray(ref int[] arr,int startIndex, int count,int value)
{
for(int i=0;i<count;i++)
{
arr[i+startIndex] = value;
}
}
and while using the function,
FillMyArray(ref temp,0,32,1);
FillMyArray(ref temp,32,64,3);
Hey flamy! I really appreciate your clear and concise answer. I will definitely be trying this out tomorrow, and will report back on how well it works out for me.
Thank you so much!!
Your answer
Follow this Question
Related Questions
Adding and Removing to a Inbuild Array 2 Answers
Variable Type for an Instance of any Script 2 Answers
how to get a variable like Layermask but for tags 2 Answers
String Access problem 1 Answer
Possible to use an array value as the name of a variable? 2 Answers