Hi, I want to load a random Level in my game, but I don't know how? I made arrays like this: Code (csharp): int[,] arrayMap1 = new int[,] { {1, 1, 1, 1, 1}, {1, 0, 0, 0, 1}, {1, 0, 0, 0, 1}, {1, 1, 1, 1, 1} }; int[,] arrayMap2 = new int[,] { {1, 1, 1, 1, 1}, {1, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {1, 0, 0, 0, 0} }; How can I do something like that? : Code (csharp): for (int i = 0; i < arrayMap+"RandomMap"; i++) { //... } RandomMap is a random number. Another example: Code (csharp): // How can I get a random array map to another array? int[,] currentMap = arrayMap+"RandomMap"; Thanks.
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.
Hi jackmott, thank you for the fast answer. For the others: A zero is missing: Code (csharp): int[,] randomMap = maps[Random.RandomRange(0, maps.Count)]; I have a another quick question. What is, when I have about 100 Levels. I add them like: Code (csharp): maps.Add(arrayMap...n); Is it then okay, or is that too much? Or should I do it differently?
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.