(CLOSED) More file handling issues. Why is working_directory giving me different results?

W

whale_cancer

Guest
Edit: Does...
For example, if you have a default *.txt file included with your game and read it into memory using working_directory, then that file will be taken from the area where the included files are stored. If you then choose to write that information to a text file using working_directory, this will write the file to the target platforms local storage, where it will be read from the next time unless you instruct GameMaker: Studio to delete the file, in which case working_directory will once more point to the area where the included files are stored.
...mean that I need to save a dummy file each time in order to get it to point to where I think it should point to? ( "C:\Users\Whale Cancer\AppData\Local\Kasei_0_22_MapEditor_Edition2\Maps\Seb\")

Edit2: I tried making a dummy file before loading so it would point to the directory I want it to. Didn't work:
Code:
        tB = file_bin_open('Dumb', 1);
        file_bin_write_byte(tB, 04);
        file_bin_close(tB);
Edit3: My loading and saving system was working before, but now I realize because I was testing it by making a blank map, editing it quickly, and then loading it. This doesn't work in the long run, of course, as it means I can only save and load within a session.

Original Post: Hello. No 'all' or '!' this time. This is driving me nuts! I know this is probably something simple like my last file handling question, but I just can't figure it out.

tldr version: Using 'working_directory + '/Maps/' + string(global.mapName) + '/' + +string(global.mapName)' gives me different locations depending on whether I am saving or loading.

Full problem:

Here is the start of my map saving script:
Code:
show_debug_message('Saving map...')

tF = file_text_open_write(working_directory + '/Maps/' + string(global.mapName) + '/' + +string(global.mapName) + '.map');
show_debug_message('Saving .map file to '+string(working_directory + '/Maps/' + string(global.mapName)) + string(global.mapName) + '.map');

file_delete(working_directory + '/Maps/' + string(global.mapName) + '/' +  string(global.mapName) + '.ini');
tIni = ini_open(working_directory + '/Maps/' + string(global.mapName) + '/' +  string(global.mapName) + '.ini');
show_debug_message('Saving .ini file to '+string(working_directory + '/Maps/' + string(global.mapName)) + string(global.mapName) + '.ini');
It creates the files and writes to them as expected. I can locate both files in "C:\Users\Whale Cancer\AppData\Local\Kasei_0_22_MapEditor_Edition2\Maps\Seb\"

Here is how I (attempt to) load the map in the map load room right before the editor:
Code:
global.mapToLoad = working_directory + 'Maps\' + parent.mapToOpen + '\'
        show_debug_message('global.mapToLoad is '+string(global.mapToLoad))
        global.loadmap = true;
  
        //resize room based on stuff
        room_set_width(rm_mapEditor, global.mapWidth * 16 * 16)
        room_set_height(rm_mapEditor, global.mapHeight * 16 * 15)
  
        room_goto(rm_mapEditor);

And then in the actual room where the map is to be loaded:

Code:
    show_debug_message('Going into rm_mapEditor and loading a map')
    show_message('Attemping to load... ' + global.mapToLoad)
    ///copy the grids from the file so that they are placed instead of the blanks created in scr_mapInitialize
    //open the file again, since we are in a new room
    tF = file_text_open_read(global.mapToLoad + string(global.mapName) + '.map');
    show_debug_message('Loading file ' + global.mapToLoad + string(global.mapName) + '.map');
You can see I use the string " working_directory + 'Maps\' + parent.mapToOpen + '\'" to point to the correct file, but when saving it points to where I expect ( "C:\Users\Whale Cancer\AppData\Local\Kasei_0_22_MapEditor_Edition2\Maps\Seb\") but when loading it decides to point to "C:\Users\Whale Cancer\AppData\Local\gm_ttt_11101\gm_ttt_42887\Maps\Seb\"

What am I doing wrong. This stuff has been frustrating me all day.
 
Last edited:

trg601

Member
I think this snippet from that very documentation might be helpful:
Working_directory can actually return two different values depending on what you are using it for. If you are writing a file to disk, working_directory points to the area of local storage that is reserved for your game on the target device (in windows this is "%LOCALAPPDATA%/gamedir/" where the "gamedir" is the directory with the name of your game). However, when reading from disk, working_directory can be either the local storage or the area where the included files are stored.
I think you should remove working_directory from your code, as I don't think it is required.
(I don't even understand why you are using working_directory at all, unless you were unaware that GMS is sandboxed, which means that if you put the path as "folder/file" it will actually get the file from your game's appdata location.)
 
W

whale_cancer

Guest
I think this snippet from documentation might be helpful:


Here is the page in documentation - working_directory

I think you should remove working_directory from your code, as I don't think it is required. (Since GMS is sandboxed, if you write a file to "folder/filename", it will write it to the game directory.)
I have looked at the documentation and quoted part of it in my problem.

Your suggestion did not work. Debug message says I loaded "Loading file Maps\6x6Defau\6x6Defau.map" File contains a series of ds_grids in string form. When trying to read them, I get nothing (so it isn't reading the file). Relevant code for how I am debugging:

Code:
    show_debug_message('Loading file ' + global.mapToLoad + string(global.mapName) + '.map');
    ds_grid_copy(global.tile_backgroundMap, file_text_read_string(tF)); file_text_readln(tF);
    show_message(string(ds_grid_write(global.tile_backgroundMap)))
 

trg601

Member
Okay, so you can open the files from windows explorer and you can see what you expect to see?

What are you saving to the files?
 
W

whale_cancer

Guest
Okay, so you can open the files from windows explorer and you can see what you expect to see?
It creates the files and writes to them as expected. I can locate both files in "C:\Users\Whale Cancer\AppData\Local\Kasei_0_22_MapEditor_Edition2\Maps\Seb\"
What are you saving to the files?
File contains a series of ds_grids in string form
The other file is an ini with some assorted values.
 
W

whale_cancer

Guest
So the problem is with the loading script, do you think you could share some more of that?
I am pretty sure I have identified the issue and posted the specific piece of code responsible with debugging information. I've bypassed my proper loading script to just a short test of being able to get the file in question open... Which I've posted upthread already.

Code:
    show_debug_message('Going into rm_mapEditor and loading a map')
    show_message('Attemping to load... ' + global.mapToLoad)
    ///copy the grids from the file so that they are placed instead of the blanks created in scr_mapInitialize
    //open the file again, since we are in a new room
    tF = file_text_open_read(global.mapToLoad + string(global.mapName) + '.map');
    show_debug_message('Loading file ' + global.mapToLoad + string(global.mapName) + '.map');
Also, I don't know why you say I shouldn't use working_directory, as if I do not, when I save, I get this error:

Code:
Error! not allowing save with filename 'C:\Maps\notgonna\notgonna.map'
Error! not allowing save with filename ''
But when I use working_directory it saves to where I expect.

The issue is that this...

Code:
 tF = file_text_open_read(global.mapToLoad + string(global.mapName) + '.map');
...points to the wrong location. That is, not the location that I wrote to.

Thus, I need to either know where to write it (and again, without working_directory in my path I get an error) or how to open the file I have already written.

You can see I use the string " working_directory + 'Maps\' + parent.mapToOpen + '\'" to point to the correct file, but when saving it points to where I expect ( "C:\Users\Whale Cancer\AppData\Local\Kasei_0_22_MapEditor_Edition2\Maps\Seb\") but when loading it decides to point to "C:\Users\Whale Cancer\AppData\Local\gm_ttt_11101\gm_ttt_42887\Maps\Seb\"
Edit: In case it wasn't clear, I am saving and loading different maps in these tests so the final directory name on my debugging information ends up being different sometimes. This is expected behavior.
 
working_directory changes based on if there is a file to load that was saved or not. If not it loads from included files. You store the original value of working_directory to a string so when you try to load the saved file, you still look in the included files for that saved file. At least that's my best guess.

Try coding working_directory to the beginning of each file name string and not saving that part to a variable. Save the path excluding working_directory and see if that works.
Code:
global.mapToLoad = 'Maps\' + parent.mapToOpen + '\'
Code:
tF = file_text_open_read(working_directory + global.mapToLoad + string(global.mapName) + '.map');
That should allow it to stay flexible and do what it is supposed to. At least if my guess is right.
 
W

whale_cancer

Guest
working_directory changes based on if there is a file to load that was saved or not. If not it loads from included files. You store the original value of working_directory to a string so when you try to load the saved file, you still look in the included files for that saved file. At least that's my best guess.

Try coding working_directory to the beginning of each file name string and not saving that part to a variable. Save the path excluding working_directory and see if that works.
Code:
global.mapToLoad = 'Maps\' + parent.mapToOpen + '\'
Code:
tF = file_text_open_read(working_directory + global.mapToLoad + string(global.mapName) + '.map');
That should allow it to stay flexible and do what it is supposed to. At least if my guess is right.
I really thought you were on to something for a minute there, but no dice. That was a problem I had when storing directories in .ini files (however, that system only worked for saving and loading within a single game session, but not between game sessions).
 
Well that's annoying. I haven't worked with included files and file read/write too much but I assumed that the dual use of working_directory could be used even with multiple game sessions. Well the only option then would be to store both working_directory values at game start to a couple global variables, try the local storage first, if -1 is returned, try the included files. If -1 is returned again, then throw an error because the file just doesn't exist.

To do that, include a blank file. Open the file for reading using working_directory. Save the path and remove the file from the string. Now open the file for writing. Use working_directory and find the saved file and store the path again to another variable. Delete the saved file. You would only have to do this at the beginning of the game and then you know you will be checking a saved files first and then included files.
 
W

whale_cancer

Guest
Well that's annoying. I haven't worked with included files and file read/write too much but I assumed that the dual use of working_directory could be used even with multiple game sessions. Well the only option then would be to store both working_directory values at game start to a couple global variables, try the local storage first, if -1 is returned, try the included files. If -1 is returned again, then throw an error because the file just doesn't exist.

To do that, include a blank file. Open the file for reading using working_directory. Save the path and remove the file from the string. Now open the file for writing. Use working_directory and find the saved file and store the path again to another variable. Delete the saved file. You would only have to do this at the beginning of the game and then you know you will be checking a saved files first and then included files.
I'll give this a try if I can't get the current method I am trying to work (included dummy files to be used as save slots). Going to mark this as closed as what I was trying to do doesn't even seem possible.
 

trg601

Member
Okay fine, I will admit something: I stay away from working_directory because I don't honestly understand why it does that either.

But you should not have to use working directory, I never use it in my GMS projects, unless I need to display the location to the player or something, which I rarely need to do.

I recommend trying (for testing purposes) to just save to the root folder of the application. So something like "file_text_open('filename')" instead of "file_text_open('map folder/filename')" and you could try putting working_directory in front, if you so want.
 
Top