Hello to Sweden. One possibility: Make sure your code has stopped reading from the array and then replace the values in the array from values you read from a file. Then let the simulation continue.
This assumes that the arrays are of the same size.
This can easily be a binary file if you only need an array of numbers like in your example.
If the player wants to go back to an earlier map then you just load its data from a file, as simple as that. There is no need to have all the maps loaded. You will want to store the data in files any ways.
I don't know your programming language but let's assume C++ now.
About loading such an array from a file... something like this:
#include <fstream>// ...// Reserve memory for the array dynamically (you can have a static array// too if you think you know already that the size is always something// specific). Notice how we combine the 2D array into a 1D array here.int* map = new int[2*2];std::ifstream fin( "my_map_file.bin", std::ios_base::in | std::ios_base::binary );if ( fin.is_open() ) { // we assume here that the file doesn't contain anything else than 2*2 ints fin.read( (char *)(map), sizeof(int) * 2 * 2 ); if ( !fin.good() ) { // well, the read operation failed. } fin.close();}// ... then you continue with the simulation// until the enddelete[] map;
To load another level you can use the same map int array if it's the same size but if not then you can delete the old one and reserve a new one of the correct size. Or if the size of the array isn't too big* then you can have it as a static array decided at compile time. In the above example the sizes 2 and 2 would not really be hard-coded (unless you want it so!) but maybe load them from your map file too.
If that doesn't help then maybe explain better what you are doing.
* The space, "the stack", reserved for such 'automatic' variables can actually be quite limited and you don't want to blow it up because that would mean your program has crashed. So when you need a big array you need to use 'new' to reserve memory for it from "the heap" and remember to use the array form of delete with the [] to free it up later.
[Edited by - reptor on October 17, 2010 6:13:42 PM]