• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [Solved] How do I use "file_text_writeln ()" without deleting the line I just jumped ???

jackrucel

Member
Hello, in the following example:

Code:
//Creat:
var ifile;

ifile = file_text_open_write("Debug.txt")
 
 file_text_write_string(ifile,"Debugtest01")
 file_text_writeln(ifile)
 file_text_write_string(ifile,"Debugtest02")
 file_text_writeln(ifile)
 file_text_write_string(ifile,"Debugtest03")
 file_text_writeln(ifile)
 file_text_write_string(ifile,"Debugtest04")
 file_text_writeln(ifile)

 file_text_close(ifile)  

//Step:
var ifile;

if keyboard_check_pressed(ord("Q")) {
 if file_exists("Debug.txt"){
 
   ifile = file_text_open_write("Debug.txt")
   file_text_writeln(ifile)
   file_text_writeln(ifile)
   file_text_writeln(ifile)
   file_text_write_string(ifile,"Debugtest999")
   file_text_close(ifile) } }
I would like to skip the first three lines without changing them, but change the fourth line to "Debugtest999", but what happens however is that the first 3 lines will be blank in .txt and the fourth line is changed.

I would like to change the fourth line without changing the first three, how do I do this with .txt files?

Thank you.

Additional information that is not required for the question:
It has to be .txt since I am not willing to use .bin and what I want to do is too big for .ini.

what am I doing:
I am making a chunks system to save the information of my world, first I create a 100x100 array to format the continent, then for each cell marked "continent" I create another 99x99 array to store information like appearance of grass, stones, etc ...

the problem is that when I tried to put all the chunks in a single array, it took forever, however, if for each chunk I create an obj and have this obj create a 99x99 array, the delay drops to maybe 30 seconds.

however creating an obj for each chunk and limiting the number of chunks to about 1800, the fps drops from 6k to 300-600, but if I only display the 9 chunks closest to the player and delete the rest I get the 6k fps back , however to delete them I need to save them so that any changes made by the player remain.

1800 chunks is not much, in my last test using 1 .txt for each chunk the following numbers will appear.

approximately 1800 .txt
approximately 300mb of ram
approximately 34mb of the hard drive

300MB ram and 34mb hard drive is nothing, my problem is the drop from 6k fps to 200-300 and the 1800 files created.

Thank you.

Edit:
Thread solved.
 
Last edited:
I

icuurd12b42

Guest
you have to load each line of the file, into a ds_list would do, and then change the line (the ds_list entry) and then save the entire list, line by line back in the file.

There is no way to change lines directly without affecting the rest of the file, you need to load it all up and re-save it all with your changes.
 

jackrucel

Member
you have to load each line of the file, into a ds_list would do, and then change the line (the ds_list entry) and then save the entire list, line by line back in the file.

There is no way to change lines directly without affecting the rest of the file, you need to load it all up and re-save it all with your changes.
I understand, I was afraid of this answer =/.

I will rewrite my codes to use ds_list and test the performance, thank you very much.
EDIT:
I just rewrote my codes and it worked perfectly in my case, I should have tried ds_grid / ds_list earlier,sorry for the unnecessary thread =/
 
Last edited:
W

Wraithious

Guest
sorry for the unnecessary thread =/
No need to be sorry, this is a good thread people can learn from, and ds lists are not the ONLY way to do it, you can also just load your file into an array, change what you want, and overwrite the file with the updated data from the updated array using a for loop and writeln
 
I

icuurd12b42

Guest
the list is the easier solution as it allows changing, adding and removing lines with ease
 
W

Wraithious

Guest
the list is the easier solution as it allows changing, adding and removing lines with ease
Possibly so, but a couple things to note, some people are more comfortable/knowledgeable using arrays than ds lists, and 2nd ds lists are volital whereas arrays are not, but yea I get what you're saying if you know your way really well with ds lists it is easier to use them.
 
I

icuurd12b42

Guest
Possibly so, but a couple things to note, some people are more comfortable/knowledgeable using arrays than ds lists, and 2nd ds lists are volital whereas arrays are not, but yea I get what you're saying if you know your way really well with ds lists it is easier to use them.
Basically same complexity, with less feature though, and basically same speed

var a;
var ct = 0;
a[ct++] = 1;
a[ct++] = 2;
a = 0; //flag array as done in garbage collector

var l = ds_list_create();
var ct = 0;
l[|ct++] = 1;
l[|ct++] = 2;
ds_list_destroy(l); //done
 

jackrucel

Member
Hi, thanks for the help guys.

I thought I would explain briefly why ds_grid in my case worked better than array, if someone is curious about.

What am I doing:
I'm using a 100x100 array to create the continent format of my game, however for each cell in the 100x100 array I decided I wanted another 100x100 array to store information like trees, rocks, monsters, etc.

But creating a single array 10,000x10,000 is unfeasible (I tried kkkk), not to mention that with the algorithm I did to create the shape of the continent, 4/6 of the cells form as ocean, and since I do not intend to use these cells, they do not need an array.

Why did ds_grid serve me better?
Since I decided to create an array for each cell in the continent shape array, I assumed (correct me if I am wrong) that I would have to manually create all the array I would use to store the cell information, unless I create an obj and in this obj create an array and for each cell create this obj.

But with the ds_grid_create I can create a ds_grid for each cell and save the id of the grid that I just created in the cell itself along with all the extra information like for example the cell type, whether it is connected or not, etc.

Attention
In no way do I believe that the way I've done is the best or the most efficient, nor am I saying that you guys have to do it this way.

I'm just reporting what worked for me. :D

Well back to coding :D

Thanks for the help guys.
 

GMWolf

aka fel666
a = 0; //flag array as done in garbage collector
You technically don't need that line as the garbage collector will kick in as soon as the array falls out of scope.

[Edit]
Well, unless you passed the array out of scope.
What I should have said is, the array would be freed as soon as no variables refer to it.
 
Last edited:
I

icuurd12b42

Guest
You technically don't need that line as the garbage collector will kick in as soon as the array falls out of scope.

[Edit]
Well, unless you passed the array out of scope.
What I should have said is, the array would be freed as soon as no variables refer to it.
You can find references to memory leaks on the forum due to array use. I don't trust the garbage collector at all.
 

GMWolf

aka fel666
You can find references to memory leaks on the forum due to array use. I don't trust the garbage collector at all.
Yeah, circular dependencies are an issue.
But the GC got improved quite a bit semi recently. (I think there was a patch note mentioning that).
 
Top