Game Development Community

Storing preferences

by Mr.Trance · in General Discussion · 01/20/2010 (4:34 am) · 2 replies

For example I want to store my game preferences/scores/money/etc, and the load it if I need.
The first idea how to do it was to store them in a file, or structured file like xml.

So, using torque script and Torque2d - how to do that? Maybe there's better solution?
How you deal with it in your games?
Is there a good tutorial to cover my question?

Thanks!

About the author

Young indie game developer from far far city.


#1
01/20/2010 (12:23 pm)
Use the FileObject() and its associated functions (openFileForRead, openFileForWrite, writeLine, readLine, close, etc) to create and read/write/append files. Once you get the hang of it, it's pretty easy.

Here's what it generally looks like:
%file = new FileObject();

if(!%file.openForWrite(%filename))
{
  %file.delete();
  return false;
}

%line = //Put stuff in here to write to the file
%file.writeLine(%line);

%file.close();
%file.delete();

Of course, you need to get the file name, which you can get using a SaveFileDialog() object (check out the editors, they have examples of it). Hope that helps.
#2
01/21/2010 (3:30 am)
Thanks a lot, Ted!

I have found myself that storing preferences and game data directly that way you mentioned isn't the best way, because reading the info inside later is complicated - you have to write functions to separate values from variables, and functions to search the necessary variables.

It's a lot better to use xml, and I have found the solution here.