Hello folks,
So I got boared late one night, and wrote a very simple, yet very easy script that writes two variables, number1 and number2, to a file, before reading them from said file and displaying them.
I've commented the code thoroughly, and you never no, someone might find it useful!
Code below:

// a small script to write some variables to a file, and then read them from said file
// by Nathan Tech
int number1=2395; // our first variable. Change this to what ever you like.
int number2=3344; // our second variable, change this to what ever you like
void main() {
show_game_window("The read write variable program with no purpose. By Nathan Tech."); // the games window message. We probably don't need this, but might as well have it anyway
alert("hello, oh almighty user!","did you know that number1="+number1+" and number2="+number2+"? Isn't that great?"); // a simple ok button to show the user what the variables are
save(); // save the variables to a file in the save function, see void save
alert("saved!","WE've saved the variables, lets load them back!"); // another alert function, because you can never have too many of those
load(); // load up the variables, see void load
alert("[wow]!","Well would you look at that! number1="+number1+" and number2="+number2+"! woo!"); // display what number 1 and number2 are now
exit(); // we're done
}
// save function next
void save() {
file f; // our file object
f.open("MyFile.txt","w"); // opens a file called MyFile.txt with the parameter "w", meaning we are writing to the file.
f.write(number1+"|"+number2); // write the variable number1, followed by a | symbol, followed by the variable number2, into the file
f.close(); // close the file
// we're done
}
// load the variables
void load() {
file f; // our file object
f.open("MyFile.txt","r"); // open up MyFile.txt, but this time, give the parameter "r", so as to read from the file.
string result; // this will be the result of our reading from the file
result=f.read(); // read the contents of MyFile.txt, and store it in the string we declared above called result
f.close(); // we don't need the file open, so lets close that
string[] myarray; // we'll need this array now, so that we can split up our string into the two original variables
myarray=string_split(result, "|", false); // divide up the result variable into segments based on where the | symbol is. This will produce an array that has the first item of number1, and the second item of number2.
// Remember, at the moment, because myarray was declared as string[], if we want to store number1, and number2, back in their int variables, which we do, we'll need to turn them back into numbers, like so
number1=string_to_number(myarray[0]); // the first item in any array is 0, followed by 1, 2, 3, ETC. This line takes the first element in the array called myarray, turns it into a number, and sets it to number1.
number2=string_to_number(myarray[1]); // does the same as the line above but for number 2
// we're done
}

Enjoy!

Thumbs up

Thank you Nathan for sharing this.
You are reely helping me

Thumbs up

Here's an alternate approach which does the reading and writing using dictionaries and the serialize/deserialize functions. When games start to get big, and need to save a lot of variables of different types, this is inevitably what you'll need to start using.
// a small script to write some variables to a file, and then read them from said file
// by Nathan Tech
//Alternative approach by Trajectory
int number1=2395; // our first variable. Change this to what ever you like.
int number2=3344; // our second variable, change this to what ever you like
void main() {
show_game_window("The read write variable program with no purpose. By Nathan Tech."); // the games window message. We probably don't need this, but might as well have it anyway
alert("hello, oh almighty user!","did you know that number1="+number1+" and number2="+number2+"? Isn't that great?"); // a simple ok button to show the user what the variables are
save(); // save the variables to a file in the save function, see void save
alert("saved!","WE've saved the variables, lets load them back!"); // another alert function, because you can never have too many of those
load(); // load up the variables, see void load
alert("[wow]!","Well would you look at that! number1="+number1+" and number2="+number2+"! woo!"); // display what number 1 and number2 are now
exit(); // we're done
}
// save function next
void save() {
file f; // our file object
f.open("MyFile.txt","wb"); // opens a file called MyFile.txt with the parameter "wb", meaning we are writing to the file in binary mode. Since dictionary output may contain characters which cannot be printed ("binary" characters), we have to add b to the mode string (the second argument to open), so it's "wb" instead of "w".
dictionary d;//a dictionary to store our variables while we prepare them for output.
d.set("number1", number1);//save the value of number1 to the dictionary under the key (name) "number1".
d.set("number2", number2);
f.write(serialize(d)); // the serialize function automatically takes care of turning our variables into one string which can be written to the file.
f.close(); // close the file
// we're done
}
// load the variables
void load() {
file f; // our file object
f.open("MyFile.txt","rb"); // open up MyFile.txt, but this time, give the parameter "rb", so as to read from the file in binary mode.
string result; // this will be the result of our reading from the file
result=f.read(); // read the contents of MyFile.txt, and store it in the string we declared above called result
f.close(); // we don't need the file open, so lets close that
dictionary d = deserialize(result);//turn the contents of result back into a dictionary
d.get("number1", number1);//retrieve the data stored under the name "number1", and copy it to the variable called number1.
d.get("number2", number2);

// we're done
}

Thumbs up