• Hey! Guest! The 37th GMC Jam will take place between May 28th, 12:00 UTC and June 1st, 12:00 UTC. Why not join in! Click here to find out more!

GMS 2 Write into a specific position of a text file

Valrad

Member
Hello,

I have a problem with the whole system of file handling of GMS2. Actually, I don't know how add things to a pre-existent text file in a specific position in it.

With the functions we already have, we can only open it, write something that will automatically delete everything else, and close it... That's not very useful...

Thanks for help
 

TailBit

Member
Hm, yeah .. I've not done much file writing in other programs, so I don't have anything to compare it to:
- open it
- read everything into a ds_list
- replace the position in the ds_list
- write the whole file again from the ds_list
- delete ds_list and close file
 

Valrad

Member
I actually thought to create a secondary file that will stock the previous version of the text file, and then to get a new one, I copy the secondary, and add new things.
 

CloseRange

Member
append only adds to the end of a file not to the middle.
Unless you plan to have extremely big files using just a plane array will suffice:
Code:
// Read File
var file = file_text_open_read("file.txt");
var len = 0;
lines[0] = "";
while(!file_text_eof(file)) {
    lines[i++] = file_text_read_string(file);
    file_text_readln(file);
}
file_text_close(file);

// -- EDIT STRING HERE

var file = file_text_open_write("file.txt");
for(var i=0; i<len; i++) {
     file_text_write_string(file, lines[i]);
     file_text_writeln(file);
}
file_text_close(file);
You could potentially create a second file if you wanted however just note that will be a lot slower on the program. This is because reading/writing to files is slow and you would be doing that twice as much.
 

Valrad

Member
I found another problem with lists now,

I create a list, and GMS2 sees it like if it wasn't...

Code:
global.logs = ds_list_create();
if (ds_exists(global.logs, ds_type_list) or is_array(global.logs)) draw_text(400, 400, "is array")
The draw_text isn't working, so I guess that GMS don't recognize it as a list
 

Valrad

Member
Oooh I see, it works if I put it in the Draw Event. But why I can't simply draw text in a different script? But fine, now I got another error xd

Code:
for (var i = 0; i < ds_list_size(global.logs); i++){
   draw_text(100, 100+5*i, string(global.logs[i]))
}
And with this, it says that "trying to index a variable which is not an array
at draw_text(100, 100+5*i, string(global.logs))"
 

chamaeleon

Member
Hello,

I have a problem with the whole system of file handling of GMS2. Actually, I don't know how add things to a pre-existent text file in a specific position in it.

With the functions we already have, we can only open it, write something that will automatically delete everything else, and close it... That's not very useful...

Thanks for help
You do understand that on a technical level you can only overwrite at some given position, not insert or delete, right? Text files have no concept of lines that can be made longer or shorter or removed or inserted. It is just a bunch of bytes. Overwriting more characters than there are in the line means overwriting the newline character and overwriting the content of the following line, etc. Ini files are no exception. Read all in, write all out after changing one or more entries.
 

TheouAegis

Member
But why I can't simply draw text in a different script?


You can!

....Except you have it set to clear the application_surface at the start of every draw phase, so even though you can draw text or whatever in the create event, it's just going to get erased. Or drawn over.
 

rIKmAN

Member
Sounds like it would benefit you to go through a couple of the official My First Game tutorials to get a grip on how the engine works, what events are used for etc.

It might sound boring, but doing that along with referencing the manual for information on general engine features and specific function definitions as you go through them will teach you the basics a lot quicker than you think.
 
Top