Add included files in code?

LawH

Member
Is this possible?

I found a really simple way to store a lot of information, but I think I would need to include files through code, rather than select them beforehand, since the file names would vary quite a lot I would imagine.
 

FoxyOfJungle

Kazan Games
game_save_id returns the full path of the directory that is used by your game to save files to. Then you can use it to place files in this program folder. Remember that this folder will vary according to what you have selected in the game settings (%appdata% or %localappdata%). Since GMS 2 is a sandboxed, you can disable this option in the settings.



Use file_ * functions to perform these operations.
 

LawH

Member
Oh, I am using GMS 1, so can I disable this sandboxing system with it? And just to make sure, the sandboxing system is the thing stopping me from wildly editing files?
 

kburkhart84

Firehammer Games
If you were willing to upgrade, from 2.3 on, included files are much easier to use than before. You just put them in the datafiles folder of the project folder. You don't have to do anything at all in the IDE. You can also change them whenever you need to and they stay updated, as the IDE no longer does any importing or anything to them like in older versions. On top of that, you get lots of other benefits from the upgrade. GMS2 is better is just about every way.
 
If you were willing to upgrade, from 2.3 on, included files are much easier to use than before. You just put them in the datafiles folder of the project folder. You don't have to do anything at all in the IDE. You can also change them whenever you need to and they stay updated, as the IDE no longer does any importing or anything to them like in older versions. On top of that, you get lots of other benefits from the upgrade. GMS2 is better is just about every way.
The system in GM:S 1 drove me insane. Coming off of GM7 where I could use the same folder across multiple projects (great for utilizing several custom editors for the game), having to deal with GM:S throwing a fit if I were to just replace a file, or accidentally delete the file or the reference while the other still exists, I was slowly starting to lose my mind, especially with how many external files my game has. It's worth upgrading to GMS2 just for that.
 

kburkhart84

Firehammer Games
The system in GM:S 1 drove me insane. Coming off of GM7 where I could use the same folder across multiple projects (great for utilizing several custom editors for the game), having to deal with GM:S throwing a fit if I were to just replace a file, or accidentally delete the file or the reference while the other still exists, I was slowly starting to lose my mind, especially with how many external files my game has. It's worth upgrading to GMS2 just for that.
The fact that updates are seamless the new way let me make my input system better too. I was previously doing settings and default configurations in strings and you would have to copy/paste those strings into your code. Now, I just make the user change a #macro that points to the project directory, and when you run the configuration stuff it automatically uses that and you never have to copy/paste settings anymore. I'm going to end up doing something similar for my audio system for where you configure the way different sounds work.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Oh, I am using GMS 1, so can I disable this sandboxing system with it? And just to make sure, the sandboxing system is the thing stopping me from wildly editing files?
What is your end goal? Adding files to the project, using external files as an easier way to store game data, loading user-generated or user-selected files, or something else entirely? The ideal solution heavily depends on the intent.

But to answer the questions, I don't recall a way to disable the sandbox in 1.x aside from extensions (e.g. GMFileSystem).
The sandbox is not the (only) thing stopping you from wildly editing files, but it is one of them. The other things are user account permissions and other related target device operating system-level security regulations.

This means that even if you do disable the sandbox, you are still not guaranteed to be able to access the files you want to access. Therefore, I believe the better approach is finding out what your end goal is, and then figuring out how to reach that goal within the boundaries of the sandbox, as that takes the gamble of whether you'll actually have access permissions out of the game.
 

LawH

Member
Unfortunately an upgrade is not an option, since I do not currently have any money for it. I have been going through c# lessons but am not anywhere near ready to port my game to to unity anytime soon.

What I intend to do at least is to create files with the file name being for example a characters name, and that file has hundreds of pieces of data, at least. And there might even be hundreds of these files.

Today I will look into using just one file, and figuring out if there is a "read_line" sort of function in GMS. I have only spent the latter part of yesterday figuring this out, but for example saving multi dimensional arrays into a file is so easy when you just write them as reals onto a file, and bring them back using for loops. It works perfectly, and I don't see why this type of saving system isn't more available, since the other saving systems I have tested have no been working at all. They demand too much input when all one really needs is a line of numbers separated by spaces. I am still new to this type of functionality, but the time I spent with python, it was fairly easy to save a long string of numbers, go through each of them in any way you wished, and place the data from different objects on different lines and so on. For example I just can't get the map ds to work at all, even when I copy pasted code and changed some of the variables to fit my needs. For me it just seems overly complicated, probably due to me being a simple guy.

But like said, I will look into this better today, I didn't really have the time yesterday evening when I started working on saving functionality.

EDIT:

Ok, so I made a temporary file, that I will write things to, and then I copied that file to another file that wasn't in the included files, but was in one of the games folders, and it seems to have worked. So are all the files in the games directory open for editing, or does this copying skip the sandbox somehow?

It also seems that this copying of the file using file_copy is not accurate. The data changes during this copying process, but it might be something on my end since I am just beginning to test things out. I don't really get how the copied file can be so different this way. The amount of data is the same, and the last two sort of test integers in the back are the same, but the rest of the data is scrambled. It consists of -1, 0, -1, so I don't get how the data changes. If you have any info on this that would be great.

I first just write onto a file, then directly copy it with no other actions between these two events.

Saving:
GML:
var file;
file = file_text_open_write("Saves/Temp/temp.ini");


for (i = 0; i < number_of_traits; i ++){
    file_text_write_real(file, array_traits[i,0]);
}
file_copy("Saves/Temp/temp.ini","Saves/Characters/" + string(name1) + string(name2) + "Traits.ini");


file_text_close(file);
Loading:

Code:
///Load Character Data
var path, file;
path = "Saves/Characters/" + string(name1) + string(name2) + "Traits.ini"
file = file_text_open_read(path);

for (i = 0; i < number_of_traits; i ++){
    array_traits[i,0] = file_text_read_real(file);
}

file_text_close(file);

EDIT2:

So I guess I don't have to add files to the "sandbox" if they are in a folder in the game directory? It seems to be working fine now with a direct writing on the file I want. Just not sure why the copying process is not working. It can't be the order of the items in the file that changes, since the last to integers are always the same. None of the integers are disappearing since there is always the exact same amount of them. It just jumbles it up for some reason.

I will have to figure that one out at some point, but for now saving and loading seems to be working this way.
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
Ok, so I made a temporary file, that I will write things to, and then I copied that file to another file that wasn't in the included files, but was in one of the games folders, and it seems to have worked. So are all the files in the games directory open for editing, or does this copying skip the sandbox somehow?
The sandbox is always in effect, unless disabled or overridden by the file dialog functions.

Why is it important for the file to be in Saves/Characters and not in Saves/Temp? Why, if the former is the desired path, do you not write the file directly to Saves/Characters?

I first just write onto a file, then directly copy it with no other actions between these two events.

Saving:
GML:
var file;
file = file_text_open_write("Saves/Temp/temp.ini");


for (i = 0; i < number_of_traits; i ++){
    file_text_write_real(file, array_traits[i,0]);
}
file_copy("Saves/Temp/temp.ini","Saves/Characters/" + string(name1) + string(name2) + "Traits.ini");


file_text_close(file);
Loading:

Code:
///Load Character Data
var path, file;
path = "Saves/Characters/" + string(name1) + string(name2) + "Traits.ini"
file = file_text_open_read(path);

for (i = 0; i < number_of_traits; i ++){
    array_traits[i,0] = file_text_read_real(file);
}

file_text_close(file);
You're copying a file without closing it first. File contents are written when the file is closed. You're copying outdated file contents.

You're also not separating the data you're saving to the file. There's no way for the engine to know what -11001-101-1-1001 is supposed to be split into, and it's going to read that back as garbage data. Add some newlines to separate the data, separate them using commas, or something like that.

So I guess I don't have to add files to the "sandbox" if they are in a folder in the game directory? It seems to be working fine now with a direct writing on the file I want. Just not sure why the copying process is not working. It can't be the order of the items in the file that changes, since the last to integers are always the same. None of the integers are disappearing since there is always the exact same amount of them. It just jumbles it up for some reason.

I will have to figure that one out at some point, but for now saving and loading seems to be working this way.
The following are accessible from within the sandbox:
- local storage (appdata) can be read from and written to
- program directory (the .exe file is here) can be read from
If a file with a given name exists in both locations, they are prioritized in that order.
 

LawH

Member
I was trying to play around with the files since I wasn't sure if sandbox meant that you can't edit any files unless you include them. That would have been horrible, since then you would not be able to create new files with random names which I can imagine would be almost a necessity.

I got it working now and I'm surprised that this isn't a common way of saving arrays. Every thread I saw about it always said don't save arrays. I'd say yes save arrays, and save them this way.

GML:
///Save Character Data
//Variables
var path, file;
//Shortcuts to Paths
path = "Saves/Characters/" + string(name1) + string(name2) + ".ini";
///Save Appearance
file = file_text_open_write(path);
///Save Names
file_text_write_string(file, p1_name1);
file_text_writeln(file);
file_text_write_string(file, p1_name2);
///Save Appearance
file_text_writeln(file);
file_text_write_real(file, p1_skin);
file_text_writeln(file);
file_text_write_real(file, p1_hair);
file_text_writeln(file);
file_text_write_real(file, p1_eyes);
///Save Traits
file_text_writeln(file);
for (i = 0; i < number_of_traits; i ++){
    file_text_write_real(file, array_traits[i,0]);
}
file_text_close(file);

I save all the data from a character on a single file, it works great, and you can load the character instantly into the game as well if you want to.
 
Top