Inserting values into a 2-D list & the type-cast error: A Noob's Doom.
Hey everyone !
Everytime my player moves I'd like to store some double-float values about where he passed into a list and later assign coins that i pick up to those values (imagine a trail of items behind a snake). Every new move creates 4 points with an X and Y value that need to be stored for later use (hence I need 2 floats per point). I've managed to create a 2 dimensional list of floats, but for some reason I get a cast error on the lines where I declare the float[,] firstSpot, float[,] secondSpot, etc, saying I can't convert a float to an int even tho it seems to me I've got floats on both sides of the line.
The second part of the code just enables picked up objects to stick to a point where the snake has passed. I hope this makes sense! Thank you!
EDIT: I initialised my arrays on top:
public List<GameObject> ballsList = new List<GameObject> ();
public List<float[,]> posList = new List<float[,]>();
then later:
public void SnakeBuilder(int dir, float pX, float pY){
if (dir == 0) {
float[,] firstSpot = new float[pX, (pY - .25f)];
float[,] secondSpot = new float[pX, (pY - .50f)];
float[,] thirdSpot = new float[pX, (pY - .75f)];
float[,] fourthSpot = new float[pX, (pY - 1f)];
posList.Insert (0, firstSpot);
posList.Insert (1, secondSpot);
posList.Insert (2, thirdSpot);
posList.Insert (3, fourthSpot);
} else {
float[,] firstSpot = new float[pX - .25f, pY];
float[,] secondSpot = new float[pX - .50f, pY];
float[,] thirdSpot = new float[pX - .75f, pY];
float[,] fourthSpot = new float[pX - 1f, pY];
posList.Insert (0, firstSpot);
posList.Insert (1, secondSpot);
posList.Insert (2, thirdSpot);
posList.Insert (3, fourthSpot);
}
for (int i = 0; i < ballsList.Count; i++) {
ballsList [i].transform.position = new Vector3 ((float)posList [i].GetValue (0), (float)posList [i].GetValue (1), 0f);
}
}
Answer by LSPressWorks · Jul 27, 2017 at 10:23 PM
INitialize the array
new float [ArraySize1,ArraySize2];
That seems to be assigning values where the compiler is expecting dimensions for your array. Whole numbers. Think of it like a box full of tiny boxes. It cannot be .25 boxes wide and 4 boxes tall.
float Whatever[0,0] = DoSomeMath();
the numbers in the brackets on either side of the comma are indices, or locations within the array. Those get values assigned.
So an array of Float[2,2] would be 2 addresses or blocks "Tall" and two wide.
Like such
[0,0][0,1]
[1,0][1,1]
So you can assign the py VALUE to the index float[0,0]
Answer by martinboisrond · Jul 28, 2017 at 02:10 PM
Thanks for your answer LSPressWorks!
Sorry I forgot to show how I initialized the Lists, i've edited my post to show it.
Ok so if I understand I went the wrong way about doing this. What would then be the best way to create and access a list of 2-floats group (coordinates on the X and Y where the objects are supposed to be to follow the head of the snake)? Also I need to .Insert the new values on the top every time the player moves so it needs to be a List.
Sorry I'm a bit confused about all this x)
Thanks a lot !
It's ok to be new, so long as you try. This isn't stack exchange, you don't have to apologize for not knowing an answer up front. You're just not likely to find much help here.
So, read up on this first. I think you're not really understanding what the array is and isn't doing. In the array you would declare, Float[x,y]; each number of x+y is like a cordinate system. Each holds 1 single float value.
A point is a non primitive data type that can hold two floats, but I really would recommend you read this bit on arrays.
This
float[,] firstSpot = new float[pX, (pY - .25f)];
can not function in any language I know of.
this on the other hand
float[,] firstSpot = new float[1,2];
firstSpot[0,0] = a single floating point number;
firstSpot[0,1] = a single floating point number;
each one of those addresses is a unique position in the array, not a set of two values. Go check this out.
Answer by JayeshModi · Dec 05, 2017 at 01:31 PM
Best part is that you have tried your best. So coming to your code I think you should read article below on array in java.
Because, after seeing your question I thought giving you a suggestion on reading article below. Since it would be helpful in future projects.