Game Development Community

Database / Writing to file

by Rarw Muffinz · in Torque 3D Beginner · 09/12/2013 (10:13 am) · 4 replies

Hey folks,
I was wondering how i could write data to a file line, Such as storing client data like Username, Level, or any string. I have no idea how I could write data to a file...
example data file:
John, 1, 13
Mary, 4, 1
ext

Thanks,
RarwMuffinz

#1
09/12/2013 (10:43 am)
Assuming you mean in TorqueScript:
function writeToFile()  
{  
    %outFileHandle = new FileObject() ;  
      
    %outFileHandle.openForWrite("myfilename.txt") ;  
      
    %outFileHandle.writeLine("I am writting this to a file") ;  
  
    %outFileHandle.close() ;  
    %outFileHandle.delete() ;         
}  
  
function readFromFile()  
{     
    %inFileHandle = new FileObject() ;  
      
    %inFileHandle.openForRead("myfilename.txt") ;  
  
    while(!%inFileHandle.IsEOF())  
    {  
        %inLine = %inFileHandle.readLine() ;  
          
                // output read text to console.  
        echo(%inLine) ;  
    }  
  
    %inFileHandle.close() ;  
    %inFileHandle.delete() ;      
}

(From: http://www.garagegames.com/community/forums/viewthread/41393)
#2
09/12/2013 (11:20 am)
Personally, when I write data to files, I make use of two very neat functions for String data, getWord and getField. This way you can have a string of data split by tabs or spaces, and can do things like:

Name \t Level \t EXP \t ect.

And then simply do:
%string = getStringCode(); //Either via file or C++, or W/e
%plName = getField(%string, 0);
%plLevel = getField(%string, 1);
%plEXP = getField(%string, 2);
.
.
.
#3
09/12/2013 (12:25 pm)
Thanks im going with Lucas' answer (seing how there are multiple clients)I have to account for
#4
09/12/2013 (12:35 pm)
So something like this?
function writeToFile(%content)    
{    
    %outFileHandle = new FileObject() ;    
        
    %outFileHandle.openForWrite("myfilename.txt") ;    
        
    %outFileHandle.writeLine(%content) ;    
    
    %outFileHandle.close() ;    
    %outFileHandle.delete() ;           
}

What i dont understand is using readfromfile in an if command
%tmp = readfromfile();
if(%tmp !$= ""){}...etc