Working directory keeps changing unpredictably

Discussion in 'Legacy GameMaker Community Tech Support' started by Zvoc47, Aug 9, 2016.

  1. Zvoc47

    Zvoc47 Guest

    Whenever I compile and run my game, the working directory changes to some random directory in local app data. Why can't the working directory be the directory where the project is? This makes me unable to load an options.txt file in which my game's settings such as player controls and language will be because the folder is always created from scratch and it's impossible for me to copypaste all the ini files into that folder before obj_OptionsManager starts looking for them.

    I'm using a legal v1.4.1757 Game Maker Studio Pro with Android Export Module all bought on Humble Bundle and GMFileSystem extension.
     
  2. Mike

    Mike nobody important GMC Elder

    Joined:
    Apr 12, 2016
    Posts:
    2,327
    Add the file as an included file, and use configs if they need to swap depending on platform. These files will then be copied over when the game is compiled.
     
    Heat4Life and ParodyKnaveBob like this.
  3. Zvoc47

    Zvoc47 Guest

    I added the file as an included file, but the game code wouldn't write anything to the INI file. Here's my code:
    Code:
    var inipath="settings.ini";
    FS_file_text_set_endl_windows();
    show_message("inipath="+inipath);
    if(FS_file_exists(inipath))
    {
        show_message("Ini file exists. Now opening it.");
        OptionsFileHandler=FS_ini_open_ext(inipath);
        show_message("SettingsFileHandler="+string(SettingsFileHandler));
    }
    else
    {
        show_message("Ini file doesn't exist. Now making it.");
        var handler=FS_file_text_open_write(inipath);
        FS_file_text_writeln(handler);
        FS_file_text_close(handler);
        SettingsFileHandler=FS_ini_open_ext(inipath);
        show_message("SettingsFileHandler="+string(SettingsFileHandler));
    }
    FS_ini_write_string_ext(SettingsFileHandler,"Test","Test","Test");
    if(show_question("Wanna close the INI file?"))
    {
        FS_ini_close_ext(SettingsFileHandler);
    }
    I've read that GMS has this weird bundling/sandboxing system because of which writing to the included files is not allowed. This code works when the file isn't included. In both cases, the compiler creates a new temporary folder in C:\Users\Zvoc47\AppData\Local\gm_ttt_somenumber named gm_ttt_someothernumber each time I click to run the game.

    I tracked down the files of the Hello Mario Engine and tried changing the program with show_message lines of code. Each time I ran or compiled the game, it perfectly saved the files to C:\Users\Zvoc47\AppData\Local\HelloMarioEngine. Why doesn't it do that to my game?

    However, if I export my game in zip format, extract it and run it, all the files go to the same folder where the game runtime is contained.

    Keep in mind that I'm using the functions of the GMFileSystem whereas the Hello Mario Engine uses the built-in file I/O functions. I need to use GMFileSystem because I want to implement a painting feature where the player can paint their own avatar sprite in the game and save it to the characters folder of the game and use it for online battles or sharing with their friends. GMFileSystem can import and export sprites as well as export surfaces that the player will paint their character on.
     
  4. Mike

    Mike nobody important GMC Elder

    Joined:
    Apr 12, 2016
    Posts:
    2,327
    Saving: The saved file always goes into the "save" area on the file system, not into the games directory. This is different on each machine.
    Loading: the "save" area is checked first, then the game directory/bundle. This allows you to load from the game folder, save it, then reload the changed file.

    I can't help with other peoples extensions though, they'll have different rules and you'll have to adapt to them. If you need to specify a full file path in GMFileSystem, then the above won't work. However, you only really need to use the above for the images themselves - not the INI files surely?
     
    ParodyKnaveBob likes this.
  5. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,866
    ParodyKnaveBob likes this.
  6. Zvoc47

    Zvoc47 Guest

    If I need a full file path, how will I get the current folder? If my game is in C:\Users\Zvoc47\Desktop\MyGame, which variable am I supposed to use to get that folder? In The Games Factory, I would simply use apppath$, but what is the equivalent for Game Maker Studio?
    However, I don't think the extensions are the problem. When I compile my game and run it, the runtime creates a new temporary folder each time and the working_folder is fixed to that temporary folder. That isn't the case in the Hello Mario Engine project where the working folder is always the %localappdata%\HelloMarioEngine folder. How can I force the working_folder to be fixed into %appdata\MyGame?
    I've seen this question answered as fixed, but it's not fixed for me! :(
    This is only working if I export the game as a ZIP archive. Only then will the game's working_directory be what I call apppath$. Unfortunately, I cannot debug my game that way.
     
  7. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,866
    You don't need any of that. If I understand your question properly, to load simply call

    Code:
    file = file_text_open_read("myfile.txt"); // or whatever... the important thing to note is that you don't need the full path
    No need for working directory. If you want to save then it would be the same... and if you want to restore a file that was included after it has been saved it would simply be:

    Code:
    file_delete("myfile.txt");
    file = file_text_open_read("myfile.txt");
    The file will be deleted from the local save directory and then loaded from the game storage directory... You don't need to know the full path at any time and GMS manages that for you.
     
  8. Zvoc47

    Zvoc47 Guest

    Thanks! It works. I will test tomorrow if it works with the ZIP format.
     
  9. Scott Ulmer

    Scott Ulmer Guest

    Hey is there anyway to load a file from a sub folder? For example I am having users pick an PNG, and it copies to the folder I can save in (working director), but I want it to be working dir + "\Images\ + file. That way it is not exactly where my EXE is. However, I can't seem to get GM to load anything in a subfolder
     
  10. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,866
    Make your own topic for your issue and post the code you are using. ;)
     
  11. OliverSB

    OliverSB Member

    Joined:
    Apr 10, 2018
    Posts:
    37
    I'm having trouble with this. Trying to execute code in the shell. Using 'working_directory + "Example.png"'.

    Simply won't work because the directory it returns doesn't correspond with the directory i passed into the surface_save function.

    My question is how do I get the directory for the file I just saved? Like I want an actual file address were I can just copy it into file explorer and press the enter key, voila.
     
  12. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,866
    Please don't necro-bump three year old topics. You'll get more and better replies making your own.
     
    Yal likes this.
  13. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,556
    Not to contribute to a necrobump, but when I'm already here and the topic hasn't been locked yet...
    Use get_save_filename? It asks the user to select a filename for saving, lets them bypass the sandbox, and gives you permission to save to that filename until the next time the game is restarted, even if the location is outside the sandbox.
     
  14. Samuel Venable

    Samuel Venable Time Killer

    Joined:
    Sep 13, 2016
    Posts:
    1,240
    But the mac implementation is rather poorly done and limiting. If anyone in this topic (i.e. the OP and whoever else) needs a fully working Windows, Mac and Linux implementation, this is free and open source: https://marketplace.yoyogames.com/assets/6621/dialog-module

    Note the GMS 1.4 edition is outdated and no longer supported, but still works just fine.
     

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