-4

I am currently working on a game project. I am using float value as a dimension as I have to store multiple pawns in a single cell of a grid.

At some point I have to declare an array location points for selection and movement purpose, so I am using this code to declare

  public PlayerScript[,] Pawns {get; set;}

but by default the value for [,] are taken as int and every time I have to typecast them to int to check the if conditions. And the assignment statement is not working. is it any alternative style to write the same thing to declare the values as float?

4
  • 1
    Could you clarify what you're asking, and provide more context? It sounds like you want the indexes of the array to be floats; as in, you want to be able to say Pawns[0.1f,3.14f]. Commented Feb 27, 2018 at 2:09
  • Yes wherever I am passing float values of [x,y] is does not accepts. Commented Feb 27, 2018 at 2:13
  • Are x and y always whole numbers? Commented Feb 27, 2018 at 2:15
  • no they are float numbers. like 0.25f, 1.75f etc Commented Feb 27, 2018 at 2:47

1 Answer 1

2

Arrays in C# can only be indexed by integer types, not floating point types. This is the norm for most programming languages, and it's not clear what the semantics would be otherwise.

I suggest reconsidering what data structures you're using. You've stated the reason you're wanting to use floats is because you want multiple objects in each cell of the grid. If that's the only reason you're using floats, then you could use a List<PlayerScript> for each cell, and each list could hold any number of PlayerScripts.

public List<PlayerScript>[,] Pawns { get; set; }

You'll need to populate each cell with an empty list object before using it.

Pawns = new List<PlayerScript>[8,8];
for (int r = 0; r < 8; r++)
{
    for (int c = 0; c < 8; c++)
    {
        Pawns[r,c] = new List<PlayerScript();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.