Game Development Community

Cannot load from AppData folder *Updated code, still stuck*

by Luis E Pabon · in Torque Game Builder · 05/08/2012 (6:23 pm) · 14 replies

I've tried saving and loading settings for a game through code, but it is not responding. I have followed the FileIO tutorial, and tried every solution in threads made in the last few years to no avail.

This is my code:
*Removed old code. New code is lower in the page*

#1
05/08/2012 (8:10 pm)
Hmm, I know I've gotten reading and writing to work... try:

if(%file.openForRead("~/data/test.txt"))
#2
05/08/2012 (9:04 pm)
That didn't work, unfortunately.

If it helps, I noticed that if I called writeFile right before loadSettings, it will find the file. It doesn't stick, however, so when I close and open the game again it cannot find it anymore.
#3
05/09/2012 (1:15 am)
I'm sorry this is beyond my scope, but could you give me a link for the FileIO tutorial please. I want to learn it, and I only found some overview about it.
#4
05/09/2012 (4:56 am)
Hmm, it should work, though I might be forgetting something else I had to do. My options set-up looks pretty similar.. just with the tilde in the file path.

function SaveDefaultOptionsReal()
{ 
%file = new FileObject();
    if(%file.openForWrite("~/data/config.txt"))
    {
     %file.writeLine($version);   
    <... lots of stuff being written>    
   announce("Options saved.");         
      %file.close();
      %file.delete();
    }
else
        {announce("Error opening file");}
}

function onStartUp()
{
<exec's and unrelated code>
%file = new FileObject();
    if(%file.openForRead("~/data/config.txt"))
    {
   %ver = %file.readLine();
   <lots of stuff being read and set-up>   
   %file.close();
   %file.delete();   
   }
   else
   {
    <default set-up code>
   }    
}

What OS are you running it on? I know that there can be some issues with folder rights on some systems>
#5
05/09/2012 (6:10 pm)
I'm running on Windows 7 64-bit.

@Vlad - http://docs.garagegames.com/tgb/official/
Check in the Scripting list.
#6
05/12/2012 (8:15 pm)
I've had no luck so far. Is there some way I can force the game to look for the data in the AppData, as it's supposed to?

Not even the commonConfig.xml file works correctly, unless I create it in TGB and then it only works because TGB generates it in the game's folder, not the AppData folder.

My biggest problem with this is that I've had none of these issues when working with iTorque, where I can create a file in a certain place and it will be there.
#7
05/15/2012 (3:11 pm)
I'm going through that http://docs.garagegames.com/tgb/official/ FileIO tutorial and having the same problems.
Such a pain in the eyes.
That Hello World! pops up in the console but the file where it is written is nowhere to be seen, I scanned the entire PC.
#8
05/15/2012 (3:16 pm)
The file should be in your AppData folder. It's hidden, so that might be why your scan didn't pick it up.

I get the reasoning behind that, but it baffles me why the file will not load when I run the "read" function.

My current project suffering from this uses the PSK, and I would like to avoid having to rebuild the solution to fix this if possible.
#9
07/16/2012 (8:21 pm)
Bumping because I still have this issue. Hopefully someone who's had this problem can provide advice.
#10
07/24/2012 (4:47 pm)
Still no luck. Is there a way I can just have the game create a folder on the user's Documents folder and dump the save data there? Almost every game I own does this, plus it's easier to access the files like that.
#11
07/26/2012 (7:24 am)
Try to drop your stuff under "game/" or "game/data/".
#12
07/26/2012 (11:45 am)
Tried, but it also gives me the same error.

Here's my current code.

function OptionsMenuGui::saveSettings(%this)
{
	$pref::Video::fullScreen = (OptionsMenuDisplayMode.getText() $= "Fullscreen");
	$pref::Video::Resolution = removeWord(OptionsMenuResolution.getText(), 1);
	
   setScreenMode(firstWord($pref::Video::Resolution), getWord($pref::Video::Resolution, 1), 32, $pref::Video::fullScreen);
	
	$pref::Audio::SFXVolume = OptionsMenuSFXVolume.getValue();
	$pref::Audio::MusicVolume = OptionsMenuMusicVolume.getValue();
	
	%this.SoundVolume = $pref::Audio::SFXVolume;
	%this.MusicVolume = $pref::Audio::MusicVolume;
	
   SoundOnce.volume = %this.SoundVolume;
   SoundLoop.volume = %this.SoundVolume;
   
   MusicLoop.volume = %this.MusicVolume;
   
   setChannelVolume($SoundManager::Sound::Channel, %this.SoundVolume);
   setChannelVolume($SoundManager::Music::Channel, %this.MusicVolume);
   
   %file = new FileObject();
   
   if(%file.openForWrite("game/optionsData.txt"))
   {
      %file.writeLine($pref::Video::fullScreen);
      %file.writeLine($pref::Video::Resolution);
      %file.writeLine($pref::Audio::SFXVolume);
      %file.writeLine($pref::Audio::MusicVolume);
      echo("Options updated");
   }else
   {
      error("file not open for writing");
   }
   
   %file.close();
   %file.delete();
   
	Canvas.popDialog(%this);
}

function OptionsMenuGui::loadSettings(%this)
{
   %file = new FileObject();
   %filename = expandFilename("game/optionsData.txt");
   
   if(%file.openForRead(%filename))
   {
      %x = 0;
      while(!%file.isEof())
      {
         switch$(%x)
         {
            case "0":
            $pref::Video::fullScreen = %file.readLine();
            case "1":
            $pref::Video::Resolution = %file.readLine();
            case "2":
            $pref::Audio::SFXVolume = %file.readLine();
            case "3":
            $pref::Audio::MusicVolume = %file.readLine();
         }
         %x++;
      }
      setScreenMode(firstWord($pref::Video::Resolution), getWord($pref::Video::Resolution, 1), 32, $pref::Video::fullScreen);

      SoundOnce.volume = $pref::Audio::SFXVolume;
      SoundLoop.volume = $pref::Audio::SFXVolume;
      
      MusicLoop.volume = $pref::Audio::MusicVolume;
   }
   else
   {
       error("Cannot read options data!");
       //error("Creating options data file");
      //
       //%initialize = new FileObject();
       //
       //if(%initialize.openForWrite(%filename))
       //{
           //%initialize.close();
           //%initialize.delete();
       //}
   }
   %file.close();
   %file.delete();
}
#13
07/27/2012 (2:18 am)
openForRead() should open the file as long as Torque can find it, there probably is no way to break this with wrong code.

Use isFile() to check if Torque can see your file. If it can right after the file was written, but cannot after restart, you may try and use openForAppend() to make Torque aware of the file again.

Or better, use SimXMLDocument to manage your settings.

#14
07/27/2012 (8:36 am)
openForAppend worked! Thank you so much! I can finally complete this part.

Will check SimXMLdocument as well.