- Home /
How do I quickly save a large number of variables?
I have a large number of variables (263,169 to be exact, what I am trying to save is the terrain heights), and every way I have tried saving them has not seemed to work because it takes way to long. Does anyone have an easy way of saving them? Thanks!
Answer by Eric5h5 · Jun 01, 2011 at 05:17 AM
Convert the 2D array of floats to a 1D arrays of bytes, then use File.WriteAllBytes with that array. (That's what Fractscape does when saving the terrain heightmap, and it takes a fraction of a second to export a 513x513 terrain.)
Could you post the code? I dont know how I to convert the 2d array of floats to a 1D array of bytes, Thanks!
@logty: The code has some project-specific stuff in it, but the idea is that you convert the 0.0-1.0 float range to a 0-65535 integer range using Lerp, then with this number (call it "p"), do "p / 256" for the low byte and "p % 256" for the high byte.
Answer by KennSan · May 31, 2011 at 07:42 PM
Have you tried using an array?
I have tried arrays, but it didn't seem to work because I can't find a way of saving a 2d array (the terrain heights). Any ideas?
Answer by Meltdown · May 31, 2011 at 07:43 PM
What are the different ways you have tried saving them? For that amount I'd suggest using a database engine of some sort.
So far I have tried using player prefs, with a huge (like 263,169 loops) for loop, but that takes ~10 $$anonymous$$utes I'd say. I have also tried using the SimpleDictionary on the Unity wiki but again the same problem. What sort of database engine do you mean?
When I mean a database engine I mean $$anonymous$$ySQL/SQL Lite etc. I assume since you are only storing terrain data on the local players computer, and not on a server somewhere, you'll be ok to talk directly to the database. Do a search on Unity answers/Google for using $$anonymous$$ySQL with Unity and you'll be good to go. Good luck :p
Answer by Igotkidsuknow · May 31, 2011 at 08:22 PM
Most people tried some sort of image map and that seems to work fine. Have you tried saving the data as a simple image object? Do a search for terrain image map here in the forums. That might help.
Could you explain some? I looked around, but I am not sure how to make a simple image object.
Answer by logty · Jun 01, 2011 at 02:43 PM
Okay, I tried making my 2d array into a 1D array, and it seems to work, but where does it save my file? Here is the code, thanks!
import System.IO;
var TerrainD:Terrain;
function Start () {
var TD = TerrainD.terrainData;
var heights:float[,] = TD.GetHeights(0,0,512,512);
var BigArray = new byte[263169];
print(BigArray.length);
var Inte = 0;
for (var y : int = 0; y < 512; ++y) {
for (var x : int = 0; x < 512; ++x) {
BigArray[Inte] = Mathf.Round(Mathf.Lerp(0,65535,heights[y,x]));
Inte+=1;
}
}
print(Time.time);
// Apply all SetPixel calls
File.WriteAllBytes( "/Hello.ini", BigArray);
}
To be more exact, where is it saving Hello.ini? Also, the code doesn't seem to be working because it doesn't seem to be creating a file.
Right now it seems it will save it to whatever location the program is using, so scour your unity folder or use the windows build in searcher.
Try file.wri$$anonymous$$llbytes("C:\\Hello.ini", BigArray);
It will save it to your C:\ Drive. Note that two \\'s are required as the first one opens a special character generator (meaning it's how you can do "\"I like you\"" to get a text or string to go "I like you".
Your answer
Follow this Question
Related Questions
Start another thread for a function 1 Answer
How to save achievments in game 2 Answers
saving highscores 1 Answer
Get Height of Terrain in Script 1 Answer
When should I save data 2 Answers