Context:
I am working on a project where I have an array of floats (assigned to a tilemap)
What I’ve been trying to do is to easely (and efficiently in performance) to save that array into a file then loading it back again.
So far I managed to make it work by saving the floats into a .txt file separated by “/” and then loading it back again ignoring those slashes. The problem is that when I save a relatively small amount of floats (250000 floats) it takes a couple of minutes to save it, (strangely enough, it loads instantaniously) and I am planning to expand it to millions of numbers. You can see where the problem is.
So what I need is either a way to directly save the array into a .txt and then load it back again
or a way to efficiently save an array of floats with slashes between them (to separate them)
Here is my code:
private void SaveTilemap()
{
string saveFile = "";
saveFile += regionMapSize.x + "/" + regionMapSize.y + "/" + regionsize + "/" + "
";
for (int regionX = 0; regionX < regionMapSize.x; regionX++)
{
for (int regionY = 0; regionY < regionMapSize.y; regionY++)
{
for (int tilemapX = 0; tilemapX < regionsize; tilemapX++)
{
for (int tilemapY = 0; tilemapY < regionsize; tilemapY++)
{
saveFile += tilemapRegionValues[regionX, regionY, tilemapX, tilemapY].ToString() + “/”;
}
}
}
}
}
private void LoadTilemap()
{
print(“Loading tilemap”);
int index1 = 0;
int index2 = 0;
string save = File.ReadAllText(Application.dataPath + “/Tilemaps/save.txt”);
index1 = save.IndexOf("/");
regionMapSize.x = int.Parse(save.Substring(0, index1));
index2 = save.IndexOf("/", index1 + 1);
regionMapSize.y = int.Parse(save.Substring(index1 + 1, index2 - index1 - 1));
index1 = save.IndexOf("/", index2 + 1);
regionsize = int.Parse(save.Substring(index2 + 1, index1 - index2 - 1));
float[,,,] loadTilemap = new float[regionMapSize.x, regionMapSize.y, regionsize, regionsize];
int lastIndex = index2 + 5;
int index = 0;
for (int regionX = 0; regionX < regionMapSize.x; regionX++)
{
for (int regionY = 0; regionY < regionMapSize.y; regionY++)
{
for (int tilemapX = 0; tilemapX < regionsize; tilemapX++)
{
for (int tilemapY = 0; tilemapY < regionsize; tilemapY++)
{
index = save.IndexOf("/", lastIndex + 1);
loadTilemap[regionX, regionY, tilemapX, tilemapY] = float.Parse(save.Substring(lastIndex + 1, index - lastIndex - 1));
lastIndex = index;
}
}
}
}
}
And by the way I separated my tilemap into “regions” to optimise it.
Don’t hesitate to ask any questions.