Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

jagged array for top 10 scores

Discussion in 'Scripting' started by Reizla, Apr 10, 2014.

  1. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    I'm trying to make an array for the top 10 scores (worldwide), which has to be read from a on-line database. The database part is no problem, but making the array to get it working is :(

    The array declaration I want to make is (taken from some 'manual'):
    Code (csharp):
    1.  
    2.     int[][] myWorldScores = new int[4][];
    3.     myWorldScores[0] = new string[10]; // Name
    4.     myWorldScores[1] = new int[10]; // High score
    5.     myWorldScores[2] = new byte[10]; // Platform
    6.     myWorldScores[3] = new string[10]; // Date
    7.  
    but each line gives me 4 errors though:

    Assets/Scripts/MainGame.cs(17,9): error CS0178: Invalid rank specifier: expected `,' or `]'
    Assets/Scripts/MainGame.cs(17,26): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
    Assets/Scripts/MainGame.cs(17,32): error CS0178: Invalid rank specifier: expected `,' or `]'
    Assets/Scripts/MainGame.cs(17,42): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration


    I'm kinda at a loss how to setup jagged arrays in C#/Unity. I've made lots of these in the past in other (non-OOP) languages without problems.

    Any help is appreciated!
     
  2. TournyMasterBot

    TournyMasterBot

    Joined:
    Sep 8, 2013
    Posts:
    13
  3. iMob

    iMob

    Joined:
    Apr 9, 2014
    Posts:
    55
    I think you are missing a value in your first line.
    You want to initialize an array of 4x10, but you only write new int [4] [].
    Try with new int [4] [10].
    But then you have an array of 4x10 integers. And you try to assign strings and bytes to it. That can not work.

    You should use a class for it:
    Build a class with all your needed values and then make an array of those class.
    Then you have 10 "packages" with all your values in ("WorldScores" in the following code)
    Code (csharp):
    1.  
    2.  
    3. public class myWorldScores {
    4.  
    5. string myWorldScoreName;
    6. int myWorldScoreScore;
    7. byte myWorldScorePlatform;
    8. string myWorldScoreDate;
    9.  
    10. }
    11.  
    12. public myWorldScores[] WorldScores = new myWorldScores[10];
    13.  
    14.  
     
    Last edited: Apr 10, 2014
  4. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    Did you read the page and check my code? I've done exactly what's written there and yet I get errors :(

    Nice thinking, but adding new int[4][10] gives me a new error:
    Assets/Scripts/MainGame.cs(16,46): error CS0178: Invalid rank specifier: expected `,' or `]'
     
  5. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    You are getting errors because you are mixing types here. The jagged array you defined is a matrix of integers, meaning you can only store ints in there. But you are trying to cram strings and bytes into the array.

    If you want to go with a jagged array you could store the data in strings:
    Code (csharp):
    1.  
    2. string[][] myWorldScores = new string[4][];
    3. myWorldScores[0] = new string[10]; // Name
    4. myWorldScores[1] = new string[10]; // High score
    5. myWorldScores[2] = new string[10]; // Platform
    6. myWorldScores[3] = new string[10]; // Date
    7.  
    Or do something like this:
    Code (csharp):
    1.  
    2. public class WorldScore
    3. {
    4.     public string name { get; set; }
    5.     public string date { get; set; }
    6.     public int highScore { get; set; }
    7.     public byte platform { get; set; }
    8. }
    9.  
    10. WorldScore[] myWorldScores = new WorldScore[ 10 ];
    11.  
     
  6. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    Thanks for the help! Going for the class WorldScore I think because I also want to use a personal top-3 score as well ;)

    Kinda odd though that a high language doesn't support jagged arrays with mixed value types while other (lower) languages do. Oh well, learned an other thing today ;-)
     
  7. TournyMasterBot

    TournyMasterBot

    Joined:
    Sep 8, 2013
    Posts:
    13
    Sorry Reizla, I'm tired and posted an unhelpful reply with only a link.

    Glad you got it sorted though.
     
  8. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    Hope I didn't sound too harsh in my reply to you, if so, it wasn't my intention but was much rather frustration that the whole thing wasn't working ;) Thanks for trying to help think of a solution <3