GML File not open for writing

S

saltysquid

Guest
so I'm making a small game and trying to save high scores but for some reason when I try to run it I get an error saying the file to save high scores isn't open for writing even doe I opened it the line before

scrFile = working_directory + "scores.txt"

// Creates a points variable any object can use
global.points = 0;

// Checks if a scores file exists, if so stores the saved scores, if not makes one and fills it with zeros
if(file_exists(scrFile)) {
file_text_open_read(scrFile);
global.score0 = file_text_read_real(scrFile);
file_text_readln(scrFile);
global.score1 = file_text_read_real(scrFile);
file_text_readln(scrFile);
global.score2 = file_text_readln(scrFile);
file_text_close(scrFile);
} else if(!file_exists(scrFile)) {
file_text_open_write(scrFile);
file_text_write_real(scrFile, 0);

file_text_writeln(scrFile);
global.score0 = 0;
file_text_write_real(scrFile, 0);
file_text_writeln(scrFile);
global.score1 = 0;
file_text_write_real(scrFile, 0);
file_text_writeln(scrFile);
global.score2 = 0;
file_text_close(scrFile)
}

the red part is what its complaining about but I assume if it wasnt already having issue there it would have them with the next times it tried to write something in

the scrFile variable is just to avoid having to put the same thing in so many times

I know this probably isn't the most efficient way to do it but I don't really care
 
J

Jafman

Guest
Hi there! Try aDifferentVariable=file_text_open_read(scrFile) then use that var instead of scrFile for everything else.
 
S

ScimitarDD

Guest
Try
scrFile = working_directory + "/scores.txt"
Also you can change
} else if(!file_exists(scrFile)) {
to
} else {
 
file_text_open_write() returns a number that you have to store in a variablerna and then you need to use that variable the rest of the functions related to your file
 

Amon

Member
In the resources tab under included files, right click and select 'Insert included file'. Select the file you want to include in your project. When you have done that use this code to open the file e.g. if your file is called level1.txt etc.

file = file_text_open_read("level1.txt");

If you want to read strings fro it, you use e.g.

StringData = file_text_read_string(file);
 
T

tserek

Guest
I would use arrays to store scores, more clear and easier to loop, especially when drawing. Also you can use ini files instead text. Hope this help.

Using text file:
Code:
var scrFile, i, file;
scrFile = string(working_directory + "\scores.txt");

// read
if file_exists(scrFile)
{
 file = file_text_open_read(scrFile);

 for (i=1; i<4; i+=1)
 {
  global.scores[i] = file_text_read_real(file);
  file_text_readln(file);
 }

 file_text_close(file);
}

// write
else
{
 file = file_text_open_write(scrFile);

 for (i=1; i<4; i+=1)
 {
  file_text_write_string(file, string(global.scores[i]));
  file_text_writeln(file);
 }

 file_text_close(file);
}
Using ini file:
Code:
var scrFile, i;
scrFile = "scores.ini";

// read
if file_exists(scrFile)
{
 ini_open(scrFile);

 for (i=1; i<4; i+=1)
 {
  global.scores[i] = ini_read_real("Scores","Score" +string(i), 0);
 }

 ini_close();
}

// write
else
{
 ini_open(scrFile);

 for (i=1; i<4; i+=1)
 {
  ini_write_real("Scores","Score" +string(i), global.scores[i]);
 }

 ini_close();
}
 
Last edited by a moderator:
S

saltysquid

Guest
I would use arrays to store scores, more clear and easier to loop, especially when drawing. Also you can use ini files instead text. Hope this help.

Using text file:
Code:
var scrFile, i, file;
scrFile = string(working_directory + "\scores.txt");

// read
if file_exists(scrFile)
{
 file = file_text_open_read(scrFile);

 for (i=1; i<4; i+=1)
 {
  global.scores[i] = file_text_read_real(file);
  file_text_readln(file);
 }

 file_text_close(file);
}

// write
else
{
 file = file_text_open_write(scrFile);

 for (i=1; i<4; i+=1)
 {
  file_text_write_string(file, string(global.scores[i]));
  file_text_writeln(file);
 }

 file_text_close(file);
}
Using ini file:
Code:
var scrFile, i;
scrFile = "scores.ini";

// read
if file_exists(scrFile)
{
 ini_open(scrFile);

 for (i=1; i<4; i+=1)
 {
  global.scores[i] = ini_read_real("Scores","Score" +string(i), 0);
 }

 ini_close();
}

// write
else
{
 ini_open(scrFile);

 for (i=1; i<4; i+=1)
 {
  ini_write_real("Scores","Score" +string(i), global.scores[i]);
 }

 ini_close();
}

Thanks this was real helpful
 
Top