GML filename_path getting files from the game data directory

Acid Reflvx

Member
Hi,
I've been creating a rhythm game with a level editor, and I've added a feature that allows you to import .ogg files from your computer as the level music. I've recently set it to encode the things you make in the editor into numbers, and then list those things in a .txt file. That way you can open the level in newer versions of the game. Doing this, I've run into a problem, how do I save the music? I could just save the directory of the music along with its name, but what if you want to share the level with your friends and they don't have the audio in the same directory as you? I got an answer about this on another thread, but it was complicated, and I didn't have motivation at the time, so I decided to save the name of the .ogg file at the beginning of the level's list. Then, when playing the level, it would look for a file with the same name as what was saved in the list. The name of the file without a directory gave me an error, so I decided to use filename_path to find the first file with the same name and return its directory. I then connected the directory to the file name and tried to play that, and now I'm getting this:
- In GameMaker, it opens up the Y:\ drive, which I don't even use, and then it gets the file from there.
- As an exe, it seems to save the ogg in its text file with all the game data, and accesses it from there.

I would be fine with this happening, but when I try playing the music in the GameMaker build, it says:
audio_create_stream : could not file file 'Y:\[gamename]_[randomspam]_[2randomletters]\Y:\[gamename]_[randomspam]_[2randomletters]\[audiofile].ogg'

If you know anything about how to retrieve files from the correct place, please share it with me. I've been thinking that maybe it should just ask what drive it should look on the first time you open the game, but I feel like people would think it's a virus, and you could have the audio on a different drive. If there aren't any great ways to do this I think I'll just do the complicated way, but I really don't want to spend a couple months on it.
Thank you!
 

Acid Reflvx

Member
Please post your code.
Here:

Opening and reading the ogg name from the txt, txt is "file"


toread = file_text_open_read(file);
if(toread == -1){
instance_destroy(self);
}

Setting the music up for playing:

global.songname = file_text_read_string(toread);

global.dir = filename_path(working_directory + global.songname);
global.songname = string(global.dir) + string(global.songname);

To play the audio:

if(global.songname != ""){
song = audio_create_stream(global.songname);
audio_sound_set_track_position(song,0);
audio_play_sound(song, 0, true);
}
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
Okay, so first of all... don't try to manually specify a path. Not only is there no need to do so, but chances are high you'll do it wrong on at least one variant of your target platform's operating system, and everything breaks as a result. You load files by providing a relative file path - relative to either the included files or the local storage.

So, if you have an audio file named song.ogg in the included files, you load it via song.ogg. If it's in a folder named music, your path is music/song.ogg. No working_directory, no filename_path, nothing fancy of that sort.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Literally like this.

So, if you have an audio file named song.ogg in the included files, you load it via song.ogg. If it's in a folder named music, your path is music/song.ogg. No working_directory, no filename_path, nothing fancy of that sort.
song.ogg as in the path is "song.ogg" as in audio_create_stream("song.ogg");.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Then how do you expect the engine to find it? :)

Take a look at the File System manual page to learn about something called the sandbox. This meanie is what prevents you from loading files from wherever you want because it feels like it. You can totally disable it in the game settings... and then risk not having your files be as close to guaranteed to be accessible as possible, so I personally recommend you don't.


In order to not tick off the sandbox (both in the literal and the proverbial sense), your audio files need to be in one of two locations: The program directory (same folder as the game's .exe file, all included files are exported to here at compile time) or the local storage (the save directory in appdata). You can copy them there manually.
 

Acid Reflvx

Member
I've been tinkering with this, I can't figure out how to copy a file to a new directory yet, but while I was playtesting something I saw this:

Error! not allowing file operation with filename 'C:\Users\User\Downloads\file.txt'.
Downloads is where I have the saved level from the editor.
Is this what's breaking it?


Edit: it's also not allowing file operation with the music file
 

TsukaYuriko

☄️
Forum Staff
Moderator
That is what's breaking it. Your game doesn't have access to that directory. The only folders your game has access to is the local storage and the folder the game's executable file resides in.
 

Acid Reflvx

Member
Thank you. Should I just find every .ogg file on the computer when the game loads up and copy them all to the local storage?
 

TsukaYuriko

☄️
Forum Staff
Moderator
No, for multiple reasons.

First of all, do you really want to search the entire hard drive for audio files and induce loading times of 10 dooms and death?

That aside, how are you planning to copy those files when your game doesn't have access to them? You can't do absolutely anything with files outside of the sandbox as long as you have the sandbox enabled, and absolutely anything includes copying them.


The easiest way remains to copy them to a folder the game can access before the game starts. Manually.
 
Top