Search Unity

Random Array Map?

Discussion in 'Scripting' started by Bongmo, Jan 22, 2014.

  1. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    Hi,

    I want to load a random Level in my game, but I don't know how?
    I made arrays like this:

    Code (csharp):
    1.  
    2.     int[,] arrayMap1 = new int[,] {
    3.         {1, 1, 1, 1, 1},
    4.         {1, 0, 0, 0, 1},
    5.         {1, 0, 0, 0, 1},
    6.         {1, 1, 1, 1, 1}
    7.         };
    8.  
    9.     int[,] arrayMap2 = new int[,] {
    10.         {1, 1, 1, 1, 1},
    11.         {1, 0, 0, 0, 0},
    12.         {1, 0, 0, 0, 0},
    13.         {1, 0, 0, 0, 0}
    14.         };
    15.  
    How can I do something like that? :

    Code (csharp):
    1.  
    2. for (int i = 0; i < arrayMap+"RandomMap"; i++)
    3. {
    4.  //...
    5. }
    6.  
    RandomMap is a random number.

    Another example:
    Code (csharp):
    1.  
    2. // How can I get a random array map to another array?
    3. int[,] currentMap = arrayMap+"RandomMap";
    4.  

    Thanks.
     
    Last edited: Jan 22, 2014
  2. jackmott

    jackmott

    Joined:
    Jan 5, 2014
    Posts:
    167
    using System.Collections.Generic;

    List <int[,]> maps = new List<int[,]>();

    maps.Add(arrayMap1);
    maps.Add(arrayMap2);
    maps.Add(arrayMap...n);

    int[,] randomMap = maps[Random.RandomRange(maps.Count)];

    Alternatively you could make a 3 dimensional array to story each 2 dimensional random map array but the syntax hurts my head.
     
  3. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    Hi jackmott,

    thank you for the fast answer.

    For the others: A zero is missing:
    Code (csharp):
    1.  
    2. int[,] randomMap = maps[Random.RandomRange(0, maps.Count)];
    3.  
    I have a another quick question. What is, when I have about 100 Levels. I add them like:
    Code (csharp):
    1.  
    2. maps.Add(arrayMap...n);
    3.  
    Is it then okay, or is that too much? Or should I do it differently?
     
    Last edited: Jan 22, 2014
  4. jackmott

    jackmott

    Joined:
    Jan 5, 2014
    Posts:
    167
    If you have 100 maps, each of which is hand crafted, you probably want to hand craft them in a text file of your own design, and then load up the maps from the file.

    Assume each map is the same dimension, your format could just be a .csv file (comma separated values) like so:

    1,1,0,0,1,1,10
    1,1,0,0,1,1,10
    1,1,0,0,1,1,10
    1,1,0,0,1,1,10
    1,1,0,0,1,1,10

    open the file, get a stream reader, make a loop that uses ReadLine() to read one line at time. use string.split(",") to break up the commas into an string array. use int.Parse() to turn the strings into numbers, then build your array up


    This keeps a huge mess of maps out of your code and makes it easier to change maps, swap new sets of maps in, etc etc. You could even build a tool to make drawing maps easier.

    Another option is use a paint program to make a black and white image to designs your maps, load that image up and convert it into your map array.
     
  5. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    Thanks, helped me a lot.