Game Development Community

Saving User Profiles

by Stanley D Chatman · in Torque Game Builder · 08/29/2006 (2:51 pm) · 6 replies

Does anyone have a way in TGB to save user profile data. Like when the player firsts starts the game they are prompted for their name which would be stored. Then as they progress in the game and achive certain goals, the game data is saved. Is using a standard text file sufficent, or will users modify this also does any XML methods exist that can be used with TGB.

#1
08/29/2006 (4:13 pm)
I don't know of a tutorial specifically for this feature, but what you want is the export function. There is a bit about export at the bottom of this tutorial. Click the link and do a search for the following string: "set up our options menu to save". The rest of the tutorial from that line on explains the export function and how to use it.

That should point you in the right direction.
#2
08/29/2006 (11:33 pm)
Below are two code samples I wrote to demonstrate the file functions. The first is simply two functions one to read a file and echo out the contents and write specific information into a file.

The second is two functions that allow you to open a file (read or write), specify a filehandle ID and reference it from anywhere in your code.

function readfile(%filename)
{
   %file = new FileObject();
   if(%file.openForRead(%filename))
   {
      while(!%file.isEOF())
      {
        %input = %file.readLine();
        echo(%input);
      }
   }
   %file.close();
   %file.delete();
}

function write(%filename)
{
   %file = new FileObject();
   %file.openforWrite(%filename);
   %file.writeLine( "This is an output file);
   %file.writeLine( "I can contain any plain text that the user wishes to output." );
   %file.close();
   %file.delete();
}


// Usage:
//     openFile("name","fileHandle","R");
//  Open a file named "name" assign a fileHandle ID of "fileHandle", open as Readonly.
//  Reference in another function:
//     fileHandle.readLine();
//
//     openfile("name","fileHandle","W");
//  Open a file named "name" assign a fileHandle ID of "fileHandle", open as Writeonly.
//  Reference in another function:
//     fileHandle.writeLine();
//
//     closeFile("fileHandle");
//  Closes the file associated with the fileHandle ID of "fileHandle"

function openFile(%filename,%IDname,%rw)
{
   %file = new FileObject(%IDname);
   if (%rw $= "R")
   {
   	if(%file.openForRead(%filename))
   	{
   		return 1;
   	}
   	else
   	{
   		return 0;
   	}
   }
   else if (%rw $= "W")
   {
   	%file.openForWrite(%filename);
   	return 1;	
	}
	else
	{
		return 0;
	}
}

function closeFile(%IDname)
{
   %IDname.close();
   %IDname.delete();
}
#3
08/29/2006 (11:34 pm)
// Torque File Operation Examples

function createfile()
{
        Error("--=[ Creating File: starter.fps/test.txt ]=--");
        
        %fw = new FileObject("FW");                     // Create File Object and give it a name for easier reference.
        FW.OpenForWrite("starter.fps/test.txt");        // Open / Create the file test.txt in starter.fps.
        FW.writeline("This is the test file.");         // Put a line of text into the file.
        FW.Close();                                     // Close the file.
}

function appendfile()
{
        Error("--=[ Appending To File: starter.fps/test.txt ]=--");
        
        %fa = new FileObject("FA");                             // Create File Object and give it a name for easier reference.
        FA.OpenForAppend("starter.fps/test.txt");               // Open the file test.txt in starter.fps to append data.
        FA.writeline("This line is appended to the file.");     // Put a line of text into the file.
        FA.Close();                                             // Close the file.        
}

function readfile()
{
        Error("--=[ Reading File: starter.fps/test.txt ]=--");
        
        %fr = new FileObject("FR");                             // Create File Object and give it a name for easier reference.
        FR.OpenForRead("starter.fps/test.txt");                 // Open the file test.txt in starter.fps to read data.
        while ( !FR.isEOF() )                                   // If we aren't at the end of the file, keep looping
        { 
                %line = FR.readline();                          // Read a line from the file
                echo(%line);                                    // Echo the line to the console
        }
        FR.Close();                                             // Close the file.        
}

function testFile()
{
        createfile();
        appendfile();
        readfile();
}
#4
08/30/2006 (2:40 am)
Thanks..

Will this file be accessible to the end user or will it get turned into a dso file a build time?
#5
08/30/2006 (3:22 am)
The tutorial I linked above explains how to export data, compile it to a dso, and read from the dso.
#6
08/30/2006 (5:22 am)
@thomas

Thanks for all your Help!