Game Development Community

dev|Pro Game Development Curriculum

Basic Save and Load Variables to a New File via FileObject

by Steve Acaster · 01/10/2012 (7:25 am) · 5 comments

It is possible to store variables, data, and any other form of text that you want, in new files that can be created on the fly. To this end, we use the services of FileObject.

You can create a new file using a custom extension - any extension you like, and write information into it. If the file does not exist FileObject will automatically create it before writing to it. We will be using the file extension:
//Torque Save Game

*.tsg doesn't mean anything, and you could equally use *.mbf (mighty badger file).

Our data is as follows:

First up, let's organize the data into an array, this will make cycling through it easier.

function CreateDataList()
      $DataList = new arrayobject();

   $DataList.add(1, red);
   $DataList.add(2, orange);
   $DataList.add(3, yellow);
   $DataList.add(4, green);
   $DataList.add(5, blue);
   $DataList.add(6, indigo);

   //the number added to the key of the array is just to them in order
   //but we'll not be using that here, just the string value

Next, let's create our new file and add each colour to it. We'll call the file "save.tsg". Create a new folder called "save" in your "game" directory so that we have somewhere to put this new file.

function WriteCustomDataFile()
   %num = $DataList.count();
   $DataList.moveFirst();//just to be certain it starts at the top

   %filename = expandFilename("save/save.tsg");
   %file = new FileObject();
   if(%file.openForWrite(%filename)) //open and write
      while(%num > 0)
         %line = $DataList.getValue($DataList.getCurrent());

   if(isObject($DataList))//don't need this now

Cool, now open the file in a text editor, Torsion, or whatever and use the old Mark1 Eyeball to see that the colours are now all listed in the file. After that, let's get Torque to read us the data back itself.

function ReadCustomDataFile()
   %num = 0;

   %filename = expandFilename("save/save.tsg");
   %file = new FileObject();
   if(%file.openForRead(%filename)) //open to read data
      while(!%file.isEOF()) //End Of File
         %line = %file.readLine();
         echo(%line);//print the line of data in the console
      echo("read failed");  
   echo("Total Lines OF Data Read = " @ %num);

If all has gone according to plan, the console will have printed each of the colours in order and the final total displayed will be six.

This has been a brief introduction to storing and retrieving data using FileObject. For more information on how Torque handles such things, check out the Torque Script Manual (in the documentation folder of your downloaded game engine) and look for "I/O" or search for "FileObject".

01/10/2012 (8:05 pm)
Thanks! Great info, I have an immediate need for this.
01/12/2012 (1:35 am)
Cool example... I always intended to save my important information as global variables and use the same method as saving preferences, but this is far more elegant.
01/24/2012 (9:11 pm)
I have never seen the arrayObject class with its crazy internal iterator. Is this in t2d?
04/05/2012 (7:01 pm)
Is it possible to make it2d/ios friendly version of this?
04/14/2013 (9:58 am)
..wrong thread sry :/