Author Topic: File Object Help  (Read 526 times)

OK so i have stole most of this from cashmodII (thanks clockturn) and it seems to only save the words "mr Accs" from
Code: [Select]
if(!isFile("config/server/mrCash.txt"))
{
%FO.openForWrite("config/server/mrCash.txt");
%FO.writeline("mr Accs");
%FO.close();
}

It won't save my clients id or cash? I have been playing with it for hours and it am getting no where. Hoping you guys can see an easy flaw. The script contains echos. The result when it runs is: echos 3 - 9 and also displays saved from the bottom (messageClient(%client,'',"Saved");)

Code: [Select]
package mrWorldSave {

function GameConnection::onClientEnterGame(%client) {
Parent::onClientEnterGame(%client);
schedule(1500, 0, relay1, %client);
}
};
activatePackage(mrWorldSave);

function relay2(%client)
{
schedule(1500, 0, GameSave, %client);
schedule(1500, 0, relay1, %client);
}

function relay1(%client)
{
schedule(1500, 0, relay2, %client);
}


function GameSave(%client) {
//read to write
%FO = new FileObject("FO");
if(!isFile("config/server/mrCash.txt"))
{
%FO.openForWrite("config/server/mrCash.txt");
%FO.writeline("mr Accs");
%FO.close();
}
if(%FO.openForRead("config/server/mrCash.txt"))
{
%FO.readline();
//active users
while(!%FO.isEOF())
{
echo(1);
%temp[%lines] = %FO.readLine();
if(getField(%temp[%lines],0) == %client.ID)
{
echo(2);
%temp[%lines] = %client.ID TAB %client.Cash;
%found = 1;
}
%lines++;
}
//new users
echo(3);
if(!%found)
{echo(4);
%temp[%lines] = %client.ID TAB %client.Cash;
%lines++;
}
echo(5);
%FO.close();
}
//write
echo(6);
for(%i=0;%i<%lines;%i++)
{echo(7);
%FO.writeline(%temp[%i]);
}
%FO.close();  
echo(8);
%FO.delete();
echo(9);
messageClient(%client,'',"Saved");
}
« Last Edit: June 11, 2011, 09:57:35 AM by tyler0 »

.openForRead does not work for ifs.

Plus, with all of your echos you need to add ""s around the numbers.

.openForRead does not work for ifs.

Plus, with all of your echos you need to add ""s around the numbers.


CashmodII has this? And it works fine?

Quote
if(%FO.openForRead("config/server/Cash.txt"))

otherwise how could i do this?
« Last Edit: June 11, 2011, 08:44:53 PM by tyler0 »

Remove the %FO.readLine(); just before and outside of the while loop, it's not meant to be there.
At the start, new FileObject("FO"); the FO bit isn't needed, just make it %FO = new FileObject();.
Your code formatting is a bit dodgy, I'd suggest you look into cleaning it up.


.openForRead does not work for ifs.

Plus, with all of your echos you need to add ""s around the numbers.
It returns 1 when you successfully open a file, so yes it can.
No, you don't need to. The only thing you need to put string tags around in Torque is strings with spaces in them.

It returns 1 when you successfully open a file, so yes it can.
No, you don't need to. The only thing you need to put string tags around in Torque is strings with spaces in them.
oh, wow. i'm very confused now, how have i not known this :(

thank you for teaching me stuff

Ok ya it seems no shortcuts for this one :/ i will rewrite it myself and see where i get. Thanks tho guys
« Last Edit: June 11, 2011, 10:39:28 PM by tyler0 »