Re: write a variable to a file and read it back in bgt

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 us er!","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 sav e() {
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 int o 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
}

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector

Reply via email to