ID:1626894
 
C# Code:
public byte[,,] data;

File.WriteAllBytes("Resources/Saves/World_data", data);


data holds x, y, z values that can be 0, 1, or 2. Apparently WriteAllBytes does not take a double or even a triple byte array. Only a single byte array. Does anyone have any ideas on how I can go about saving this data so I don't have to recreate it each time I run the script?

It'd save a lot of load time since using a Perlin Noise script to randomly generate the data actually makes it take longer. Especially when it's a big map.
Loop over it?

How you go about doing this depends on whether or not your 3D array here is jagged. This would handle jagged 3D arrays:

// Assuming you've got X, Y and Z variables for dimensions of the prior array
byte[] result = new byte[X*Y*Z];
int i = 0;
for (int x = 0 ; x != X ; x++)
for (int y = 0 ; y != Y ; y++)
for (int z = 0 ; z != Z ; z++)
result[i++] = data[x,y,z];
File.WriteAllBytes("Resources/Saves/World_data", result);


In response to Stephen001
Stephen001 wrote:
Loop over it?

How you go about doing this depends on whether or not your 3D array here is jagged. This would handle jagged 3D arrays:

> // Assuming you've got X, Y and Z variables for dimensions of the prior array
> byte[] result = new byte[X*Y*Z];
> int i = -1;
> for (int x = 0 ; x != X ; x++)
> for (int y = 0 ; y != Y ; y++)
> for (int z = 0 ; z != Z ; z++)
> result[i++] = data[x,y,z];
> File.WriteAllBytes("Resources/Saves/World_data", result);
>


I changed something in the script. Wouldn't doing result[i++] cause it to start at 1 instead of 0? i is now -1.

In this script, these would be written in result in the current order:
0,0,0 = result 1
0,0,1 = result 2
0,0,2 = result 3
0,0,1000 = result 1001
0,1,0 = result 1002
0,1,1000 = 2002
1000,1000,1000 = result 1,003,003,001

Honestly, I thought about looping.. but.. If I ever wanted to make a vast map and store it somewhere, I just know that in the long run something going to get utterly messy. I've made a <code>GameObject</code> list that was also as big as this list (1000,1000,1000) but it was empty. I guess allocating itself would also allocate the memory regardless and simply drop the FPS, making the entire script useless.

While your method of going about this is definitely a good one for a small map (64,64,64 or something) I wanted to know if there was any way to go about doing this with a <code>Generic List<>()</code>. I've tried doing this with the GameObject to save some memory. However, I was coming across this error:

error CS1061: Type `System.Collections.Generic.List<UnityEngine.GameObject>' does not contain a definition for `GetComponent' and no extension method `GetComponent' of type `System.Collections.Generic.List<UnityEngine.GameObject>' could be found (are you missing a using directive or an assembly reference?)


How am I going about this?

[ln 9:World.cs]public List<GameObject>[,,] chunks;

[ln 112:World.cs]chunks[x,y,z] = Instantiate(chunk, new Vector3(x * chunkSizeX - 0.5f, y * chunkSizeY + 0.5f, z * chunkSizeZ - 0.5f), Quaternion.identity) as GameObject;

[ln 117:World.cs]Chunk newChunkScript = chunks[x,y,z].GetComponent("Chunk") as Chunk;


Summary of what I just asked:

How can I access the contents of a List<GameObject>[,,] and save the contents of a List<byte>[,,] or do you have any thoughts on how to solve this memory issue?

The GameObject list only changes as a player moves. It's called chunks and chunks are deleted as a player moves away and created as a player moves closer to a new one. Only rendering the once within the player's range.

The byte list only changes as a player moves. It's called data and it is added to over time. However, once the data is added, it stays, being available to be used again when the user may go to that x,y,z location some other time. Eventually, the entire 1000,1000,1000 map will be explored and the data list will be 100% generated, which will take some time. But, wouldn't this still use up a lot of memory?

Please, if you have any solutions to solving some of these memory allocation issues, let me know.
Never mind. I've talked it over with some other guys, Ss4toby and another programmer of mine, and it seems that creating a GameObject[,,] array isn't heavy on the memory allocation since it's pass by reference and not by value. Byte isn't pass by reference and in turn has 0's as the default value. So, we're thinking about a neat way to store the x,y,z values of the byte array so that we don't have 100000000 files for a simple 1000x1000x1000 map. Do you have any ideas on this matter, Stephen? That is, what would be a neat way of saving the bytes in a file or a little bit less files.