• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Working directory keeps changing unpredictably

Z

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.
 
Z

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.
 

Mike

nobody important
GMC Elder
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?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Z

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.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
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.
 
Z

Zvoc47

Guest
Thanks! It works. I will test tomorrow if it works with the ZIP format.
 
S

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
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
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
Make your own topic for your issue and post the code you are using. ;)
 

OliverSB

Member
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.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
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.
Make your own topic for your issue and post the code you are using. ;)
Please don't necro-bump three year old topics. You'll get more and better replies making your own.
 
  • Like
Reactions: Yal

Yal

šŸ§ *penguin noises*
GMC Elder
Not to contribute to a necrobump, but when I'm already here and the topic hasn't been locked yet...
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.
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.
 
S

Sam (Deleted User)

Guest
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.
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.
 
Top