Question - Code [SOLVED] Write file outside sandbox (GMS2)

Discussion in 'GameMaker Studio 2 Community Tech Support' started by ExtremeCheddar, Sep 6, 2019.

  1. ExtremeCheddar

    ExtremeCheddar Member

    Joined:
    Sep 6, 2019
    Posts:
    4
    Hello!
    Since Game Maker Studio 2 now has the ability to disable the sandbox, I am now able to work on the built in level editor for my current game project. (It was possible before but the file restriction made it circuitous)

    I was succesful trying to read content of a text file that was pre-created on my desktop. No problem at all.
    However, when I'm trying to create a text file on the desktop (or any other directory outside the sandbox), it
    still doesn't work. No file will be created.

    Is this an undocumented limitation that prevents games from damaging anything or is there something wrong with my code?

    Code:
    var file=file_text_open_write(program_directory+"test.txt");
    file_text_write_string(file,"hello world");
    file_text_close(file);
    
    There is nothing wrong with the program_directory formatting. Debugging shows a correct path.
    Thanks for your help!
     
  2. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,408
    1. Check your combined path (does program_directory end with a slash?)
    2. If that's not the right directory, try filename_dir(parameter_string(0))
     
  3. ExtremeCheddar

    ExtremeCheddar Member

    Joined:
    Sep 6, 2019
    Posts:
    4
    My combined path is correct. I can read from that path but i can't write to it. Maybe there is something wrong with GMS2.

    When i create an executable and write to a file without a path (filename only), it will create the file inside the sandbox.
    When i read from a file (again filename only), it reads from the relative path where my executable exists. (outside the sandbox)

    Maybe there is some workaround via file_copy or something. Will try it later
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,552
    Turning off the sandbox does nothing about UAC virtualization at the system level. So if your product is intended to be run off Program Files, don't ever attempt writing in program_directory unless you know it is run with admin privileges. It will demonstrate the read-only behaviour you described.
     
  5. ExtremeCheddar

    ExtremeCheddar Member

    Joined:
    Sep 6, 2019
    Posts:
    4
    Ok it is not possible to write files outside the sandbox, even if it's disabled.

    My executable exists on my desktop;
    Code:
    file_text_open_read("read.txt"); //Opens read.txt on my desktop
    
    file_text_open_write("write.txt"); //Opens write.txt from sandbox (inside appdata or appdata/local)
    
    It doesn't matter if i use the full path via working_directory or program_directory. everything leads to the same result.

    Maybe it's an overseen bug or an intended feature.
    I also tried to copy the written file from the sandbox to my desktop. No success.

    Running the game with admin privileges didn't help either.

    In my case it's more important to read files outside the sandbox (map files from my built in level editor) but it's kinda annoying to prompt users who just created a new map, to copy the generated file to the game's actual map folder by hand...

    Still, thank you for your replies!
     
    Last edited: Sep 7, 2019
  6. ExtremeCheddar

    ExtremeCheddar Member

    Joined:
    Sep 6, 2019
    Posts:
    4
    I found the solution:
    you CAN write files outside the sandbox. There seems to be a formatting issue with working_directory and program_directory.
    Both use backslashes but you will need forward slashes to create a valid path.

    Code:
    file_text_write_string("C:/Users/SomeDude/Desktop/test.txt"); //will write test.txt on the desktop
    file_text_write_string("C:\Users\SomeDude\Desktop\test.txt"); //will do nothing
    
    working_directory and program_directory are interpreted with backslashes and will do not work.

    However, using the function get_save_filename() to set the file name and location will work aswell.

    Thanks for your help.
     
  7. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,552
    The reason that your second attempt doesn't work is because backslashes are used as escape characters in GMS 2, so the single backslashes aren't genuine and the resulting path wouldn't exist. This is a major difference compared to GMS 1, where backslashes do not have this role.

    This would have worked:
    Code:
    var f = file_text_open_write("C:\\Users\\SomeDude\\Desktop\\test.txt");
    file_text_write_string(f, "foo");
    file_text_close(f);
    
    As would have this:
    Code:
    var f = file_text_open_write("C:/Users/SomeDude/Desktop/test.txt");
    file_text_write_string(f, "foo");
    file_text_close(f);
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice