Wintermute Engine Forum

Wintermute Engine => Technical forum => Topic started by: pakhpakh64 on April 25, 2013, 12:07:12 PM

Title: problem with opening a text file for reading
Post by: pakhpakh64 on April 25, 2013, 12:07:12 PM
hi every body
I have a very irritating problem with File.OpenAsText, I have written an "AfterLoad" event for my game in witch I have a piece of code like this:
Code: WME Script
  1. on "AfterLoad"
  2. {
  3.         SomeWindow.ApplyEvent("Load");
  4. }

and in 'SomeWindow.script' :
Code: WME Script
  1. on "Load"
  2. {
  3.         var ToolStringFILE = new File(Game.SaveDirectory + "\GameDebugModeToolStrings.txt");
  4.         if(ToolStringFILE.OpenAsText(1))
  5.         {
  6.                 // some code here, concerning reading from 'ToolStringFILE'
  7.                 ToolStringFILE.Close();
  8.         }
  9.         else
  10.         {
  11.                 Game.LOG("can't open the file");
  12.         }
  13. }

The first time "AfterLoad" is run every thing work fine, but if I attempt to run the "AfterLoad" a second time, I always end up with "can't open the file" line in wme.log,
I don't know what stops the scripting from opening file.
any help would be much appreciated, please tell me if you need more information.
Title: Re: problem with opening a text file for reading
Post by: 2.0 on April 25, 2013, 03:57:56 PM
Hmm. Strange issue. Is the file exisits after reading from it?
Try to use something like this for first time:

Code: WME Script
  1. on "Load"
  2. {
  3.         global ToolStringFILE;
  4.         if (ToolStringFILE == null)
  5.              ToolStringFILE = new File(Game.SaveDirectory + "\GameDebugModeToolStrings.txt");
  6.  
  7.         if(ToolStringFILE.OpenAsText(1))
  8.         {
  9.                 // some code here, concerning reading from 'ToolStringFILE'
  10.                 ToolStringFILE.Close();
  11.         }
  12.         else
  13.         {
  14.                 Game.LOG("can't open the file");
  15.         }
  16. }

Also try to check attribute AccessMode before trying to open file. Maybe file already opened.
Title: Re: problem with opening a text file for reading
Post by: dongo on April 25, 2013, 04:12:55 PM
Not ear me this lines

i think that the problem is the "AfterLoad" event.

This event launch when the game start, how you launch the event for second time?? ...or when you launch the game for second time... 

i think that something like this is the problem..

sorry for my bad english

edit: bad response, this problem happen you when you load a save game, no?
Title: Re: problem with opening a text file for reading
Post by: Mnemonic on April 25, 2013, 04:52:50 PM
Is the file guaranteed to exist? Perhaps try adding something like Game.LOG(Game.FileExists(Game.SaveDirectory + "\GameDebugModeToolStrings.txt"));
Title: Re: problem with opening a text file for reading
Post by: pakhpakh64 on April 26, 2013, 05:49:12 AM
1. the file exist after the first time (though I didn't check it with script like what Mnemonic said, I checked the file directory).
2. I have created sort of a small tool for my game to jump through different situation in scene/puzzles, which is based on loading previously saved games. The first time     I use the tool to load a scene the tool reads the file just fine, but the second time it loads an old version of the file which has been used when creating that save.
3. I pretty much hope it's what 2.0 said, I mean could it be when ToolStringFILE.Close() is executed, the File handle is also destroyed?

btw thanks for the reply.
Title: Re: problem with opening a text file for reading
Post by: Mnemonic on April 26, 2013, 06:42:41 AM
but the second time it loads an old version of the file which has been used when creating that save.
I don't think that's technically possible.

Keep in mind when loading a saved game you're also possibly loading old scripts (the ones that were running at the time you were saving the game). Could that be the problem?
Title: Re: problem with opening a text file for reading
Post by: pakhpakh64 on May 19, 2013, 05:07:01 AM
yes that was it, thanks a lot Mnemonic.