2017-05-10 01:01:56

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!

Nathan Smith
Managing Director of Nathan Tech
It's not disability
It's ability!

2017-05-10 14:57:11

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

best regards
never give up on what ever you are doing.

2017-05-10 18:24:32

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
}

Official server host for vgstorm.com and developer of the Manamon 2 netplay server.
PSA: sending unsolicited PMs or emails to people you don't know asking them to buy you stuff is disrespectful. You'll just be ignored, so don't waste your time.

2017-06-27 01:11:18 (edited by Reza.jdp 2017-06-27 01:12:11)

hi,
well if you're coding an online game in BGT, you'd better not use dictionaries, because they're really slow in loading.
but here, at post number1:
you'd better set number 1 and number 2 to 0 before loading them back from the file.
because now, with your code, even if you don't call the load function, you can see the numbers in their original value.
try it!
just comment the load function in your main loop using // and you'll see what i'm talking about.
here's a little modification on your code:

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
// to make sure we're really loading back the variables from the file, we should set them to 0
number1=0;
number2=0;
alert("test", "number1="+number1+" and number2="+number2);
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
}

Co-founder of Sonorous Arts.
Check out Sonorous Arts on GitHub: https://github.com/sonorous-arts/