openForRead(<filename>)

Author Topic: openForRead(<filename>)  (Read 1290 times)

Code: [Select]
%filename = "./aValidFile.txt";
%file = new FileObject();
if(%file.openForRead(%filename))
   return true;
else
   return false;

%file.close();
%file.delete();

assuming aValidFile.txt exists in the same folder as the script being executed, why does it always return false?  Is there a new security implementation or am I being handicapped?
« Last Edit: August 11, 2008, 12:56:55 PM by Irk89 »

This is a snippet from Pickle's V8 Cash-Mod. I found it very useful for my old Tropical RPG when saving and loading user information. I took out some extraneous information that could only cause confusion.

Code: [Select]
function serverCmdSaveBank(%client)
{
%file = new FileObject();
%file.openforRead("./AValidFile.txt");
while(!%file.isEOF())
{
%line = %file.readLine();
switch$(firstWord(%line))
{
case "Other": %wordToWrite = restWords(%line);
default: %wordToWrite = "World!";
}
}
%file.openforRead("./AValidFile.txt");
%file.writeline("Hello SPC %wordToWrite);
%file.close();
%file.delete();
}

I wish I could provide a more case-sensitive answer, but I'm not so enlightened on file editing, yet.

Did you make sure there weren't any other file objects trying to read AValidFile.txt at the same time? It could be that the file has already been "reserved" and opened. Make sure you close/delete the fileobjects when you are done with them.

I was actually thinking about this earlier. Because of the way v9 handles add-ons, wouldn't the other txt files need to be inside your add-on zip?

I was thinking about it because my Cashmod sort of needs saving of stuff, in such a way that allows creation of new files on-the-fly. I imagine adding multiple currencies every server start would be frustrating, and bank saves will also depend on the currencies loaded.

Well, if you try to write to any files, writing to Add-Ons/Script_CashMod/Blah.txt won't work - you'll end up with it in a separate folder, not the zip file.

RAR files are not meant to be written to like that. Use folders for data storage.

Did you make sure there weren't any other file objects trying to read AValidFile.txt at the same time? It could be that the file has already been "reserved" and opened. Make sure you close/delete the fileobjects when you are done with them.

I've got blockland at a blank slate, and there is nothing else reserving that file.  I'm also using it in a folder, not in a zip file.
« Last Edit: August 11, 2008, 01:51:02 PM by Irk89 »

And I have solved the puzzle! When you are opening a file, it must be an absolute file path.  For example
Code: [Select]
%filename = "./aValidFile.txt";
This file would be located in your initial blockland directory alongside blockland.exe

Code: [Select]
%filename = "./Add-Ons/Script_Irk89/aValidFile.txt";
That would be located in your add-ons/script_irk89 folder.

Excellent. That clears my mind about that bad thought. Now I can get on with writing a long script without testing it and then sorting out lots of errors. Yay!  :cookieMonster: