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 <em>.transform.position = new Vector3 ((float)posList _.GetValue (0), (float)posList *.GetValue (1), 0f);*_</em>

* }*

* }*
[98564-screen-shot-2017-07-27-at-163754.png|98564]

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]

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 !

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.

Arrays in java.

2d array in java