Files are, of course, very useful and an alternative to the register, which offers less flexibility. Files are mainly used for saving, long-term saving but also temporary notes that your program or game needs to take. Creating those files might be hard though, using the functions that GML offers. This is how to place the information where you really want it, in the right order.
file = file_text_open_write(“myfile.myextension”);
This is the start of it all. If the file myfile.myextension doesn’t exist, it will create it. If it does exist, remember that you will now overwrite the old file! File_text_open_append is an alternative function, but this is irrelevant for this topic. The next functions you will call, when writing to a file, are these:
file_text_write_string(file,value);
file_text_writeln(file);
Never forget file_text_writeln! That is the most common thing people around the GMC forget and it will cause failure, no matter what. Now as these (and the respective function for real values) functions are the only that exists to write to file, as you will realize, there’s no “go back and edit” chance; you need to write it right the first time. The file is 100% chronological – therefore you need to make up a structure from the very first beginning to which you will keep. It can’t be like “user enters his name, it’s Charles, let’s write that to the file”… You got to save all of your information until the very last moment and follow the structure that you have decided:
file_text_write_string(file,name);
file_text_writeln(file);
file_text_write_string(file,age);
file_text_writeln(file);
file_text_write_string(file,gender);
file_text_writeln(file);
file_text_write_string(file,preferencies);
file_text_writeln(file);
This will be easy to read; you know that the first line is always the name, the second the age and the third is the gender and so on. There is also another method which is commonly used, which works with predefined values, and when you read that value on the one of the lines you know what are coming next. One of my programs got an index row at the top that defines all of the lines below. Just don’t try to write more than one value per row at the beginning, this will make it harder for you. If you want your files to look more professional you might add encryption, which will probably compress your file into as few lines as possible any way. I would suggest that you use one of the excellent DLL’s that exists for encryption. These are both fast and safe and a lot better than what you could ever come up with using GML.
.
Users logged in:
Comments
Loading comments...