Game Development Community

File read/write

by Howard Dortch · in Torque Game Engine · 07/29/2004 (7:41 am) · 10 replies

I wanted to just write some data out to a text file and read it in on game load so I looked around in the code and can't get it to work. I looked in the engine code and found this.

bool FileObject::openForRead(const char* /*fileName*/)
{
AssertFatal(false, "Error, not yet implemented!");
return false;
}

Yet in the examples in MissionInfo.cs files it's being used.

Like to have someone explain how this works since the function only does an assert and returns false.

#1
07/29/2004 (7:59 am)
The ConsoleMethod openForRead calls object->readMemory(argv[2]);
#2
07/29/2004 (8:10 am)
//-------------------------------------------
// The following two funcs should ideally save/load arrays of characters,
// but for the moment we'll take the easy way out, and just use one
function WriteLocalCharacterFile()
{
// Write the players name
   $file = new FileObject();
   $file.openForWrite("rw/client/namefile");
   $file.writeLine($pref::Player::Name);
   $file.writeLine($pref::Player::Password);
   $file.close();
   $file.delete();
}

function ReadLocalCharacterFile()
{
 // Read the name
    $file = new FileObject();
    $file.openForRead("rw/client/namefile");
    $pref::Player::Name = $file.readLine();
    $pref::Player::Password = $file.readLine();
    $file.close();
    $file.delete();
// notes: There are also openForRead,openForAppend, readLine and isEof methods.
}
I read in the info during loadMainMenu() and write it out in onExit()
It pretty much follows the same logic as reading/writing client preferences.
#3
07/29/2004 (8:14 am)
Given Erik's reply, this post was redundant.
#4
07/29/2004 (9:32 am)
Erik that works great, thanks.

I still have problem with the CC code in the engine. How does the openForRead work since the all the code does is return a false?
#5
07/29/2004 (10:32 am)
Quote:
I still have problem with the CC code in the engine. How does the openForRead work since the all the code does is return a false?

Expanding on my previous post.
Methods that you call on an object in script are defined by using the ConsoleMethod macro. The ConsoleMethod for FileObject::openForRead is defined as follows:
ConsoleMethod( FileObject, openForRead, bool, 3, 3, "(string filename)")
{
   return object->readMemory(argv[2]);
}

A quick glance or the readMemory() function seems like it simply reads the entire file into memory. Seems like a quick hack and noone ever finished writing the openForRead() C++ method.
So you cant call fileObject.openForRead() from source, but calling it from script leads to it running the readMemory() method.
#6
03/31/2008 (12:05 am)
Hi,

I am having a problem getting to write to a file. I am using the FileObject just as mentioned in this post. But the file just doesn't seem to be getting written. I tried the same code with TGE 1.5.2 and it works like a charm. I am using TGB 1.5.1 . Does anybody else have this problem? any suggestions? any solutions.. I am posting some part of my code

function writestats()
{
   %file = new FileObject();
   %file.openForWrite("test/test.txt");
   %file.writeLine("Name:" SPC $pref::Player::Name);
   %file.writeLine("Password:" SPC $pref::Player::Password);
   %file.writeLine("Bounty:" SPC $TotalScore);
   %file.close();
   %file.delete();
}
I assume this file should be written at the base_directory/test from the executable that i am running. Thats atleast how its being written in TGE 1.5.2.
#7
03/31/2008 (1:17 am)
Hi,

I am having a problem getting to write to a file. I am using the FileObject just as mentioned in this post. But the file just doesn't seem to be getting written. I tried the same code with TGE 1.5.2 and it works like a charm. I am using TGB 1.5.1 . Does anybody else have this problem? any suggestions? any solutions.. I am posting some part of my code

function writestats()
{
   %file = new FileObject();
   %file.openForWrite("test/test.txt");
   %file.writeLine("Name:" SPC $pref::Player::Name);
   %file.writeLine("Password:" SPC $pref::Player::Password);
   %file.writeLine("Bounty:" SPC $TotalScore);
   %file.close();
   %file.delete();
}
I assume this file should be written at the base_directory/test from the executable that i am running. Thats atleast how its being written in TGE 1.5.2.
#8
07/06/2008 (4:13 pm)
Can someone whip together a tutorial on how to setup the players name and password via gui and how to store it to a text file server side,?
#9
07/06/2008 (5:40 pm)
@Phil:

You would just use the previous example, but with arguments that would be passed to the method when your user clicks a 'submit' button.
#10
07/06/2008 (8:36 pm)
I guess my next question is , after searching the forums. how do i tie in guitexteditctrl boxs to a button that when you click it submits a function like

submitinfo(%playername,%playerpassword);
{
open a file named "players/playername.txt"
compare password to the first line in the file if a match then load mission
if not a match then go back to main menu with error message
}