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!
Managing Director of Nathan Tech
It's not disability
It's ability!