A little something something I created last week. I figured I might as well share it.
I'm a big fan of Jestermon's Global Hash Pool, but I don't like the fact that it's based on the global strings, so I thought it might be informative to go ahead and try to write my own file save/open system.
So I present to you: 'Save2File', a handy system to save and load your variables to a file.
Manual:
1. Use 'S2FLoadFile()' to load a file into the project.
2. Use the 'S2F...Get()' functions to read your data from the 'cloud'.
3. Use the 'S2F...Set()' functions to save your data to the 'cloud'.
4. Finally, use 'S2FSaveFile()' to save the 'cloud' to a file.
An example of usage is attached.
EDIT: Thought I might need to say this: I am aware my logic is very similar to Jestermon's. I have no intention of presenting her work as mine, nor neglecting her great work.
I'm a big fan of Jestermon's Global Hash Pool, but I don't like the fact that it's based on the global strings, so I thought it might be informative to go ahead and try to write my own file save/open system.
So I present to you: 'Save2File', a handy system to save and load your variables to a file.
Manual:
1. Use 'S2FLoadFile()' to load a file into the project.
2. Use the 'S2F...Get()' functions to read your data from the 'cloud'.
3. Use the 'S2F...Set()' functions to save your data to the 'cloud'.
4. Finally, use 'S2FSaveFile()' to save the 'cloud' to a file.
Code: [Select]
///==================================================================
///Save 2 File - A handy data save/load system
///==================================================================
string[] buffer(0);
///String Splitter
string[] S2FStringSplitter(string input, string key)
{
string[] strList(0);
string start, end, temp = input;
int i = 0, pos = 0;
while (true)
{
pos = iStringFind(temp,key,0,true);
if (pos != -1)
{
strList.resize(i+1);
iStringLeft(start,temp,pos-1);
iStringRight(end,temp,iStringLen(temp)-pos);
temp = end;
strList[i] = start;
i ++; if (i > 1000) break;
}
else
{
strList.resize(i+1);
strList[i] = end;
break;
}
}
return strList;
}
///Get Line Number
int S2FGetID(string key, bool add)
{
int l = iStringLen(key);
if (buffer.length() == 0)
{
buffer.resize(1);
return 0;
}
for (int i=0; i<buffer.length(); i++)
{
string temp;
iStringLeft(temp,buffer[i],l+1);
if (temp == key+";") return i;
else if (i == buffer.length()-1 && add)
{
buffer.resize(i+2);
return i+1;
}
}
return -1;
}
///Load File
bool S2FLoadFile(string path)
{
int file = iFileReadOpen(path);
buffer.resize(1000);
if (file > -1)
{
int i = 0;
string data = " ";
while (data != "")
{
iFileStringRead(file,data);
if (data != "")
{
buffer[i] = data;
i ++;
}
}
buffer.resize(i);
iFileClose(file);
}
else
{
buffer.resize(0);
return false;
}
return true;
}
///Save File
bool S2FSaveFile(string path)
{
int file = iFileWriteOpen(path);
if (file == -1) return false;
else
{
for (int i=0; i<buffer.length(); i++)
{
iFileStringWrite(file,buffer[i],true);
}
iFileClose(file);
}
return true;
}
///Load Float
float S2FFloatGet(string key)
{
int id = S2FGetID(key, false);
if (id == -1) return -1;
else
{
string[] list = S2FStringSplitter(buffer[id],";");
return iStringVal(list[1]);
}
}
///Save Float
void S2FFloatSet(string key, float value)
{
int id = S2FGetID(key, true);
///test = id;
if (id != -1) buffer[id] = key + ";" + value;
}
///Load String
string S2FStringGet(string key)
{
int id = S2FGetID(key, false);
if (id == -1) return "Error";
else
{
string[] list = S2FStringSplitter(buffer[id],";");
return list[1];
}
}
///Save String
void S2FStringSet(string key, string value)
{
int id = S2FGetID(key, true);
if (id != -1) buffer[id] = key + ";" + value;
}
///Load Vector
Vector3 S2FVectorGet(string key)
{
int id = S2FGetID(key, false);
if (id == -1) return Vector3(-1,-1,-1);
else
{
string[] list = S2FStringSplitter(buffer[id],";");
return Vector3(iStringVal(list[1]), iStringVal(list[2]), iStringVal(list[3]));
}
}
///Save Vector
void S2FVectorSet(string key, Vector3 value)
{
int id = S2FGetID(key, true);
if (id != -1) buffer[id] = key + ";" + value.x + ";"+value.y + ";" + value.z;
}
///Load Quaternion
Quaternion S2FQuaternionGet(string key)
{
int id = S2FGetID(key, false);
Quaternion q; iQuaternionFromEulerAngles(q,-1,-1,-1,"xyz");
if (id == -1) return q;
else
{
string[] list = S2FStringSplitter(buffer[id],";");
q.x = iStringVal(list[1]); q.y = iStringVal(list[2]); q.z = iStringVal(list[3]); q.w = iStringVal(list[4]);
return q;
}
}
///Save Quaternion
void S2FQuaternionSet(string key, Quaternion value)
{
int id = S2FGetID(key, true);
if (id != -1) buffer[id] = key + ";" + value.x + ";"+value.y + ";" + value.z + ";" + value.w;
}
///==================================================================
///==================================================================
An example of usage is attached.
EDIT: Thought I might need to say this: I am aware my logic is very similar to Jestermon's. I have no intention of presenting her work as mine, nor neglecting her great work.